{"id":81,"date":"2021-10-24T17:26:05","date_gmt":"2021-10-24T09:26:05","guid":{"rendered":"https:\/\/www.zhangshuwei.com\/?p=81"},"modified":"2021-10-24T17:26:40","modified_gmt":"2021-10-24T09:26:40","slug":"jquery%e9%80%89%e6%8b%a9%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.zhangshuwei.com\/index.php\/2021\/10\/24\/jquery%e9%80%89%e6%8b%a9%e5%99%a8\/","title":{"rendered":"jQuery\u9009\u62e9\u5668"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript line-numbers\">jQuery \u7684\u9009\u62e9\u5668\u53ef\u8c13\u4e4b\u5f3a\u5927\u65e0\u6bd4\uff0c\u8fd9\u91cc\u7b80\u5355\u5730\u603b\u7ed3\u4e00\u4e0b\u5e38\u7528\u7684\u5143\u7d20\u67e5\u627e\u65b9\u6cd5 \r\n \r\n$(\"#myELement\")    \u9009\u62e9id\u503c\u7b49\u4e8emyElement\u7684\u5143\u7d20\uff0cid\u503c\u4e0d\u80fd\u91cd\u590d\u5728\u6587\u6863\u4e2d\u53ea\u80fd\u6709\u4e00\u4e2aid\u503c\u662fmyElement\u6240\u4ee5\u5f97\u5230\u7684\u662f\u552f\u4e00\u7684\u5143\u7d20 \r\n$(\"div\")           \u9009\u62e9\u6240\u6709\u7684div\u6807\u7b7e\u5143\u7d20\uff0c\u8fd4\u56dediv\u5143\u7d20\u6570\u7ec4 \r\n$(\".myClass\")      \u9009\u62e9\u4f7f\u7528myClass\u7c7b\u7684css\u7684\u6240\u6709\u5143\u7d20 \r\n$(\"*\")             \u9009\u62e9\u6587\u6863\u4e2d\u7684\u6240\u6709\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u8fd0\u7528\u591a\u79cd\u7684\u9009\u62e9\u65b9\u5f0f\u8fdb\u884c\u8054\u5408\u9009\u62e9\uff1a\u4f8b\u5982$(\"#myELement,div,.myclass\") \r\n \r\n\u5c42\u53e0\u9009\u62e9\u5668\uff1a \r\n$(\"form input\")         \u9009\u62e9\u6240\u6709\u7684form\u5143\u7d20\u4e2d\u7684input\u5143\u7d20 \r\n$(\"#main > *\")          \u9009\u62e9id\u503c\u4e3amain\u7684\u6240\u6709\u7684\u5b50\u5143\u7d20 \r\n$(\"label + input\")     \u9009\u62e9\u6240\u6709\u7684label\u5143\u7d20\u7684\u4e0b\u4e00\u4e2ainput\u5143\u7d20\u8282\u70b9\uff0c\u7ecf\u6d4b\u8bd5\u9009\u62e9\u5668\u8fd4\u56de\u7684\u662flabel\u6807\u7b7e\u540e\u9762\u76f4\u63a5\u8ddf\u4e00\u4e2ainput\u6807\u7b7e\u7684\u6240\u6709input\u6807\u7b7e\u5143\u7d20 \r\n$(\"#prev ~ div\")       \u540c\u80de\u9009\u62e9\u5668\uff0c\u8be5\u9009\u62e9\u5668\u8fd4\u56de\u7684\u4e3aid\u4e3aprev\u7684\u6807\u7b7e\u5143\u7d20\u7684\u6240\u6709\u7684\u5c5e\u4e8e\u540c\u4e00\u4e2a\u7236\u5143\u7d20\u7684div\u6807\u7b7e \r\n \r\n\u57fa\u672c\u8fc7\u6ee4\u9009\u62e9\u5668\uff1a \r\n$(\"tr:first\")               \u9009\u62e9\u6240\u6709tr\u5143\u7d20\u7684\u7b2c\u4e00\u4e2a \r\n$(\"tr:last\")                \u9009\u62e9\u6240\u6709tr\u5143\u7d20\u7684\u6700\u540e\u4e00\u4e2a \r\n$(\"input:not(:checked) + span\")   \r\n \r\n\u8fc7\u6ee4\u6389\uff1achecked\u7684\u9009\u62e9\u5668\u7684\u6240\u6709\u7684input\u5143\u7d20 \r\n \r\n$(\"tr:even\")               \u9009\u62e9\u6240\u6709\u7684tr\u5143\u7d20\u7684\u7b2c0\uff0c2\uff0c4... ...\u4e2a\u5143\u7d20\uff08\u6ce8\u610f\uff1a\u56e0\u4e3a\u6240\u9009\u62e9\u7684\u591a\u4e2a\u5143\u7d20\u65f6\u4e3a\u6570\u7ec4\uff0c\u6240\u4ee5\u5e8f\u53f7\u662f\u4ece0\u5f00\u59cb\uff09 \r\n \r\n$(\"tr:odd\")                \u9009\u62e9\u6240\u6709\u7684tr\u5143\u7d20\u7684\u7b2c1\uff0c3\uff0c5... ...\u4e2a\u5143\u7d20 \r\n$(\"td:eq(2)\")             \u9009\u62e9\u6240\u6709\u7684td\u5143\u7d20\u4e2d\u5e8f\u53f7\u4e3a2\u7684\u90a3\u4e2atd\u5143\u7d20 \r\n$(\"td:gt(4)\")             \u9009\u62e9td\u5143\u7d20\u4e2d\u5e8f\u53f7\u5927\u4e8e4\u7684\u6240\u6709td\u5143\u7d20 \r\n$(\"td:ll(4)\")              \u9009\u62e9td\u5143\u7d20\u4e2d\u5e8f\u53f7\u5c0f\u4e8e4\u7684\u6240\u6709\u7684td\u5143\u7d20 \r\n$(\":header\") \r\n$(\"div:animated\") \r\n\u5185\u5bb9\u8fc7\u6ee4\u9009\u62e9\u5668\uff1a \r\n \r\n$(\"div:contains('John')\") \u9009\u62e9\u6240\u6709div\u4e2d\u542b\u6709John\u6587\u672c\u7684\u5143\u7d20 \r\n$(\"td:empty\")           \u9009\u62e9\u6240\u6709\u7684\u4e3a\u7a7a\uff08\u4e5f\u4e0d\u5305\u62ec\u6587\u672c\u8282\u70b9\uff09\u7684td\u5143\u7d20\u7684\u6570\u7ec4 \r\n$(\"div:has(p)\")        \u9009\u62e9\u6240\u6709\u542b\u6709p\u6807\u7b7e\u7684div\u5143\u7d20 \r\n$(\"td:parent\")          \u9009\u62e9\u6240\u6709\u7684\u4ee5td\u4e3a\u7236\u8282\u70b9\u7684\u5143\u7d20\u6570\u7ec4 \r\n\u53ef\u89c6\u5316\u8fc7\u6ee4\u9009\u62e9\u5668\uff1a \r\n \r\n$(\"div:hidden\")        \u9009\u62e9\u6240\u6709\u7684\u88abhidden\u7684div\u5143\u7d20 \r\n$(\"div:visible\")        \u9009\u62e9\u6240\u6709\u7684\u53ef\u89c6\u5316\u7684div\u5143\u7d20 \r\n\u5c5e\u6027\u8fc7\u6ee4\u9009\u62e9\u5668\uff1a \r\n \r\n$(\"div[id]\")              \u9009\u62e9\u6240\u6709\u542b\u6709id\u5c5e\u6027\u7684div\u5143\u7d20 \r\n$(\"input[name='newsletter']\")    \u9009\u62e9\u6240\u6709\u7684name\u5c5e\u6027\u7b49\u4e8e'newsletter'\u7684input\u5143\u7d20 \r\n \r\n$(\"input[name!='newsletter']\") \u9009\u62e9\u6240\u6709\u7684name\u5c5e\u6027\u4e0d\u7b49\u4e8e'newsletter'\u7684input\u5143\u7d20 \r\n \r\n$(\"input[name^='news']\")         \u9009\u62e9\u6240\u6709\u7684name\u5c5e\u6027\u4ee5'news'\u5f00\u5934\u7684input\u5143\u7d20 \r\n$(\"input[name$='news']\")         \u9009\u62e9\u6240\u6709\u7684name\u5c5e\u6027\u4ee5'news'\u7ed3\u5c3e\u7684input\u5143\u7d20 \r\n$(\"input[name*='man']\")          \u9009\u62e9\u6240\u6709\u7684name\u5c5e\u6027\u5305\u542b'news'\u7684input\u5143\u7d20 \r\n \r\n$(\"input[id][name$='man']\")    \u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5c5e\u6027\u8fdb\u884c\u8054\u5408\u9009\u62e9\uff0c\u8be5\u9009\u62e9\u5668\u662f\u5f97\u5230\u6240\u6709\u7684\u542b\u6709id\u5c5e\u6027\u5e76\u4e14\u90a3\u4e48\u5c5e\u6027\u4ee5man\u7ed3\u5c3e\u7684\u5143\u7d20 \r\n \r\n\u5b50\u5143\u7d20\u8fc7\u6ee4\u9009\u62e9\u5668\uff1a \r\n \r\n$(\"ul li:nth-child(2)\"),$(\"ul li:nth-child(odd)\"),$(\"ul li:nth-child(3n + 1)\") \r\n \r\n$(\"div span:first-child\")          \u8fd4\u56de\u6240\u6709\u7684div\u5143\u7d20\u7684\u7b2c\u4e00\u4e2a\u5b50\u8282\u70b9\u7684\u6570\u7ec4 \r\n$(\"div span:last-child\")           \u8fd4\u56de\u6240\u6709\u7684div\u5143\u7d20\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u7684\u6570\u7ec4 \r\n$(\"div button:only-child\")       \u8fd4\u56de\u6240\u6709\u7684div\u4e2d\u53ea\u6709\u552f\u4e00\u4e00\u4e2a\u5b50\u8282\u70b9\u7684\u6240\u6709\u5b50\u8282\u70b9\u7684\u6570\u7ec4 \r\n \r\n\u8868\u5355\u5143\u7d20\u9009\u62e9\u5668\uff1a \r\n \r\n$(\":input\")                  \u9009\u62e9\u6240\u6709\u7684\u8868\u5355\u8f93\u5165\u5143\u7d20\uff0c\u5305\u62ecinput, textarea, select \u548c button \r\n \r\n$(\":text\")                     \u9009\u62e9\u6240\u6709\u7684text input\u5143\u7d20 \r\n$(\":password\")           \u9009\u62e9\u6240\u6709\u7684password input\u5143\u7d20 \r\n$(\":radio\")                   \u9009\u62e9\u6240\u6709\u7684radio input\u5143\u7d20 \r\n$(\":checkbox\")            \u9009\u62e9\u6240\u6709\u7684checkbox input\u5143\u7d20 \r\n$(\":submit\")               \u9009\u62e9\u6240\u6709\u7684submit input\u5143\u7d20 \r\n$(\":image\")                 \u9009\u62e9\u6240\u6709\u7684image input\u5143\u7d20 \r\n$(\":reset\")                   \u9009\u62e9\u6240\u6709\u7684reset input\u5143\u7d20 \r\n$(\":button\")                \u9009\u62e9\u6240\u6709\u7684button input\u5143\u7d20 \r\n$(\":file\")                     \u9009\u62e9\u6240\u6709\u7684file input\u5143\u7d20 \r\n$(\":hidden\")               \u9009\u62e9\u6240\u6709\u7c7b\u578b\u4e3ahidden\u7684input\u5143\u7d20\u6216\u8868\u5355\u7684\u9690\u85cf\u57df \r\n \r\n\u8868\u5355\u5143\u7d20\u8fc7\u6ee4\u9009\u62e9\u5668\uff1a \r\n \r\n$(\":enabled\")             \u9009\u62e9\u6240\u6709\u7684\u53ef\u64cd\u4f5c\u7684\u8868\u5355\u5143\u7d20 \r\n$(\":disabled\")            \u9009\u62e9\u6240\u6709\u7684\u4e0d\u53ef\u64cd\u4f5c\u7684\u8868\u5355\u5143\u7d20 \r\n$(\":checked\")            \u9009\u62e9\u6240\u6709\u7684\u88abchecked\u7684\u8868\u5355\u5143\u7d20 \r\n$(\"select option:selected\") \u9009\u62e9\u6240\u6709\u7684select \u7684\u5b50\u5143\u7d20\u4e2d\u88abselected\u7684\u5143\u7d20 \r\n \r\n  \r\n \r\n\u9009\u53d6\u4e00\u4e2a name \u4e3a\u201dS_03_22\u2033\u7684input text\u6846\u7684\u4e0a\u4e00\u4e2atd\u7684text\u503c\r\n$(\u201dinput[@ name =S_03_22]\u201c).parent().prev().text() \r\n \r\n\u540d\u5b57\u4ee5\u201dS_\u201d\u5f00\u59cb\uff0c\u5e76\u4e14\u4e0d\u662f\u4ee5\u201d_R\u201d\u7ed3\u5c3e\u7684\r\n$(\u201dinput[@ name ^='S_']\u201c).not(\u201d[@ name $='_R']\u201c) \r\n \r\n\u4e00\u4e2a\u540d\u4e3a radio_01\u7684radio\u6240\u9009\u7684\u503c\r\n$(\u201dinput[@ name =radio_01][@checked]\u201c).val(); \r\n \r\n  \r\n \r\n  \r\n \r\n$(\"A B\") \u67e5\u627eA\u5143\u7d20\u4e0b\u9762\u7684\u6240\u6709\u5b50\u8282\u70b9\uff0c\u5305\u62ec\u975e\u76f4\u63a5\u5b50\u8282\u70b9\r\n$(\"A>B\") \u67e5\u627eA\u5143\u7d20\u4e0b\u9762\u7684\u76f4\u63a5\u5b50\u8282\u70b9\r\n$(\"A+B\") \u67e5\u627eA\u5143\u7d20\u540e\u9762\u7684\u5144\u5f1f\u8282\u70b9\uff0c\u5305\u62ec\u975e\u76f4\u63a5\u5b50\u8282\u70b9\r\n$(\"A~B\") \u67e5\u627eA\u5143\u7d20\u540e\u9762\u7684\u5144\u5f1f\u8282\u70b9\uff0c\u4e0d\u5305\u62ec\u975e\u76f4\u63a5\u5b50\u8282\u70b9 \r\n \r\n1. $(\"A B\") \u67e5\u627eA\u5143\u7d20\u4e0b\u9762\u7684\u6240\u6709\u5b50\u8282\u70b9\uff0c\u5305\u62ec\u975e\u76f4\u63a5\u5b50\u8282\u70b9 \r\n \r\n\u4f8b\u5b50\uff1a\u627e\u5230\u8868\u5355\u4e2d\u6240\u6709\u7684 input \u5143\u7d20 \r\n \r\nHTML \u4ee3\u7801: \r\n \r\n&lt;form>\r\n&lt;label>Name:&lt;\/label>\r\n&lt;input name=\"name\" \/>\r\n&lt;fieldset>\r\n      &lt;label>Newsletter:&lt;\/label>\r\n      &lt;input name=\"newsletter\" \/>\r\n&lt;\/fieldset>\r\n&lt;\/form>\r\n&lt;input name=\"none\" \/> \r\njQuery \u4ee3\u7801: \r\n \r\n$(\"form input\") \r\n\u7ed3\u679c: \r\n \r\n[ &lt;input name=\"name\" \/>, &lt;input name=\"newsletter\" \/> ] \r\n \r\n2. $(\"A>B\") \u67e5\u627eA\u5143\u7d20\u4e0b\u9762\u7684\u76f4\u63a5\u5b50\u8282\u70b9 \r\n\u4f8b\u5b50\uff1a\u5339\u914d\u8868\u5355\u4e2d\u6240\u6709\u7684\u5b50\u7ea7input\u5143\u7d20\u3002 \r\n \r\nHTML \u4ee3\u7801: \r\n \r\n&lt;form>\r\n&lt;label>Name:&lt;\/label>\r\n&lt;input name=\"name\" \/>\r\n&lt;fieldset>\r\n      &lt;label>Newsletter:&lt;\/label>\r\n      &lt;input name=\"newsletter\" \/>\r\n&lt;\/fieldset>\r\n&lt;\/form>\r\n&lt;input name=\"none\" \/> \r\njQuery \u4ee3\u7801: \r\n \r\n$(\"form > input\") \r\n\u7ed3\u679c: \r\n \r\n[ &lt;input name=\"name\" \/> ] \r\n \r\n3. $(\"A+B\") \u67e5\u627eA\u5143\u7d20\u540e\u9762\u7684\u5144\u5f1f\u8282\u70b9\uff0c\u5305\u62ec\u975e\u76f4\u63a5\u5b50\u8282\u70b9 \r\n\u4f8b\u5b50\uff1a\u5339\u914d\u6240\u6709\u8ddf\u5728 label \u540e\u9762\u7684 input \u5143\u7d20 \r\n \r\nHTML \u4ee3\u7801: \r\n \r\n&lt;form>\r\n&lt;label>Name:&lt;\/label>\r\n&lt;input name=\"name\" \/>\r\n&lt;fieldset>\r\n      &lt;label>Newsletter:&lt;\/label>\r\n      &lt;input name=\"newsletter\" \/>\r\n&lt;\/fieldset>\r\n&lt;\/form>\r\n&lt;input name=\"none\" \/> \r\njQuery \u4ee3\u7801: \r\n \r\n$(\"label + input\") \r\n\u7ed3\u679c: \r\n \r\n[ &lt;input name=\"name\" \/>, &lt;input name=\"newsletter\" \/> ] \r\n \r\n \r\n4. $(\"A~B\") \u67e5\u627eA\u5143\u7d20\u540e\u9762\u7684\u5144\u5f1f\u8282\u70b9\uff0c\u4e0d\u5305\u62ec\u975e\u76f4\u63a5\u5b50\u8282\u70b9 \r\n\u4f8b\u5b50\uff1a\u627e\u5230\u6240\u6709\u4e0e\u8868\u5355\u540c\u8f88\u7684 input \u5143\u7d20 \r\n \r\nHTML \u4ee3\u7801: \r\n \r\n&lt;form>\r\n&lt;label>Name:&lt;\/label>\r\n&lt;input name=\"name\" \/>\r\n&lt;fieldset>\r\n      &lt;label>Newsletter:&lt;\/label>\r\n      &lt;input name=\"newsletter\" \/>\r\n&lt;\/fieldset>\r\n&lt;\/form>\r\n&lt;input name=\"none\" \/> \r\njQuery \u4ee3\u7801: \r\n \r\n$(\"form ~ input\") \r\n\u7ed3\u679c: \r\n \r\n[ &lt;input name=\"none\" \/> ] <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[25],"class_list":["post-81","post","type-post","status-publish","format-standard","hentry","category-front-end","tag-jquery"],"_links":{"self":[{"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/posts\/81","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":2,"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":83,"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/posts\/81\/revisions\/83"}],"wp:attachment":[{"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}