{"id":953,"date":"2010-03-13T13:35:00","date_gmt":"2010-03-13T05:35:00","guid":{"rendered":""},"modified":"2013-11-25T20:53:54","modified_gmt":"2013-11-25T12:53:54","slug":"jquery%e9%81%8d%e5%8e%86","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/953.html","title":{"rendered":"JQuery\u904d\u5386"},"content":{"rendered":"<p>\u73b0\u5728\uff0c\u60a8\u5df2\u7ecf\u5b66\u4f1a\u5982\u4f55\u641c\u7d22\u548c\u8fc7\u6ee4\u9875\u9762\u4e0a\u7684\u6240\u6709\u5143\u7d20\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u4e00\u79cd\u9ad8\u6548\u7684\u65b9\u6cd5\u6765\u904d\u5386\u7ed3\u679c\uff0c\u8fdb\u4e00\u6b65\u5904\u7406\u5143\u7d20\u3002\u81ea\u7136\uff0cjQuery \u63d0\u4f9b\u4e86\u51e0\u79cd\u904d\u5386\u641c\u7d22\u7ed3\u679c\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u7b2c\u4e00\u4e2a\u4e5f\u662f\u6700\u5e38\u7528\u7684\u904d\u5386\u65b9\u6cd5\u662f each() \u51fd\u6570\u3002\u8fd9\u548c \u201cfor loop\u201d \u7684\u529f\u80fd\u662f\u4e00\u6837\u7684\uff0c\u904d\u5386\u6bcf\u4e2a\u5143\u7d20\u5e76\u901a\u8fc7\u8fed\u4ee3\u9012\u589e\u5143\u7d20\u3002\u6b64\u5916\uff0c\u5faa\u73af\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u7684\u5f15\u7528\u53ef\u4ee5\u901a\u8fc7 \u201cthis\u201d\uff08\u7528\u4e8e\u4e00\u822c\u7684 JavaScript \u8bed\u6cd5\uff09\u6216 $(this)\uff08\u7528\u4e8e jQuery \u547d\u4ee4\uff09\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u770b\u770b\u4e0b\u9762\u7684\u793a\u4f8b\u3002<\/p>\n<p>\u6e05\u5355 10. each \u5faa\u73af<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/ Will loop through each &lt;p&gt; tag on the page.  notice the use of the\r\n\/\/ inline function here -- this is analogous with the anonymous classes in Java.\r\n\/\/ You can either call a separate function, or write an inline function like this.\r\n\r\nvar increment = 1;\r\n$(&quot;p&quot;).each(function(){\r\n\r\n    \/\/ now add a paragraph count in front of each of them.  Notice how we use the\r\n    \/\/ $(this) variable to reference each of the paragraph elements individually.\r\n\r\n    $(this).text(increment + &quot;. &quot; + $(this).text());\r\n    increment++;\r\n});\r\n<\/pre>\n<p>\u56e0\u4e3a\u641c\u7d22\u7ed3\u679c\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u60a8\u80af\u5b9a\u5e0c\u671b\u51fd\u6570\u904d\u5386\u8be5\u6570\u7ec4\uff0c\u5c31\u50cf\u5904\u7406\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u7684\u6570\u636e\u5bf9\u8c61\u4e00\u6837\u3002\u56e0\u6b64\uff0c\u8981\u67e5\u627e\u4e00\u4e2a\u7ed9\u5b9a\u641c\u7d22\u7ed3\u679c\u7684\u957f\u5ea6\uff0c\u5219\u53ef\u4ee5\u5728\u8be5\u6570\u7ec4\u4e0a\u8c03\u7528 $().length\u3002\u6e05\u5355 11 \u5c55\u793a\u4e86\u66f4\u591a\u7684\u6570\u7ec4\u904d\u5386\u51fd\u6570\uff0c\u53ef\u9002\u7528\u4e8e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u7684\u6570\u7ec4\u904d\u5386\u3002<\/p>\n<p>\u6e05\u5355 11. \u5176\u4ed6\u6570\u7ec4\u51fd\u6570<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/ the eq() function lets you reference an element in the array directly.\r\n\/\/ In this case, it will get the 3rd paragraph (0 referenced of course) and hide it\r\n$(&quot;p&quot;).eq(2).hide();\r\n\r\n\/\/ The slice() function lets you input a start and an end index in the array, to\r\n\/\/ create a subset of the array.  This will hide the 3rd through 5th paragraphs on the\r\n\/\/ page\r\n$(&quot;p&quot;).slice(2,5).hide();\r\n <\/pre>\n<p>\u9664\u4e86\u8fd9\u4e9b\u6570\u7ec4\u904d\u5386\u51fd\u6570\u4e4b\u5916\uff0cjQuery \u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\uff0c\u4f7f\u60a8\u53ef\u4ee5\u67e5\u627e\u5d4c\u5957\u5728\u641c\u7d22\u8bcd\u5468\u56f4\u7684\u5143\u7d20\u3002\u4e3a\u4ec0\u4e48\u8fd9\u5f88\u6709\u7528\u5462\uff1f\u4f8b\u5982\uff0c\u6211\u4eec\u5e38\u5e38\u9700\u8981\u5728\u56fe\u7247\u7684\u65c1\u8fb9\u5d4c\u5165\u4e00\u4e2a\u6587\u672c\u6807\u7b7e\uff0c\u6216\u5728\u8868\u5355\u5143\u7d20\u65c1\u8fb9\u5d4c\u5165\u4e00\u4e2a\u9519\u8bef\u6d88\u606f\u3002\u4f7f\u7528\u8fd9\u4e9b\u547d\u4ee4\u53ef\u4ee5\u67e5\u627e\u7279\u5b9a\u7684\u8868\u5355\u5143\u7d20\uff0c\u7136\u540e\u901a\u8fc7\u5c06\u8868\u5355\u5143\u7d20\u653e\u7f6e\u5728\u4e0b\u4e00\u4e2a\u5143\u7d20\uff08span \u6807\u8bb0\uff09\u4e2d\uff0c\u628a\u8be5\u9519\u8bef\u6d88\u606f\u76f4\u63a5\u653e\u7f6e\u5728\u8868\u5355\u5143\u7d20\u65c1\u8fb9\u3002\u6e05\u5355 12 \u663e\u793a\u4e86\u8fd9\u79cd\u8bbe\u8ba1\u7684\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<p>\u6e05\u5355 12. \u793a\u4f8b next() \u51fd\u6570<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;input class=validate type=text&gt;\r\n\r\nfunction validateForm()\r\n{\r\n    $(&quot;.validate:text&quot;).each(function(){\r\n    if ($(this).val()==&quot;&quot;)\r\n    \/\/  We'll loop through each textfield on the page with a class of &quot;validate&quot;\r\n    \/\/  and if they are blank, we will put text in the &lt;span&gt; immediately afterwards\r\n    \/\/  with the error message.\r\n\r\n        $(this).next().html(&quot;This field cannot be blank&quot;);\r\n});\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u73b0\u5728\uff0c\u60a8\u5df2\u7ecf\u5b66\u4f1a\u5982\u4f55\u641c\u7d22\u548c\u8fc7\u6ee4\u9875\u9762\u4e0a\u7684\u6240\u6709\u5143\u7d20\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u4e00\u79cd\u9ad8\u6548\u7684\u65b9\u6cd5\u6765\u904d\u5386\u7ed3\u679c\uff0c\u8fdb\u4e00\u6b65\u5904\u7406\u5143\u7d20\u3002\u81ea\u7136\uff0cjQ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-953","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=953"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/953\/revisions"}],"predecessor-version":[{"id":5233,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/953\/revisions\/5233"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}