{"id":2921,"date":"2024-11-10T14:58:39","date_gmt":"2024-11-10T06:58:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2921\/"},"modified":"2024-11-10T14:58:39","modified_gmt":"2024-11-10T06:58:39","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e8%bf%99%e6%ae%b5-javascript-%e4%bb%a3%e7%a0%81%e4%b8%ad%e7%9a%84-i-%e5%a7%8b%e7%bb%88%e8%be%93%e5%87%ba-6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2921\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u8fd9\u6bb5 JavaScript \u4ee3\u7801\u4e2d\u7684 `i` \u59cb\u7ec8\u8f93\u51fa 6\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173094174545075.jpg\" class=\"aligncenter\" title=\"\u4e3a\u4ec0\u4e48\u8fd9\u6bb5 JavaScript \u4ee3\u7801\u4e2d\u7684 `i` \u59cb\u7ec8\u8f93\u51fa 6\uff1f\u63d2\u56fe\" alt=\"\u4e3a\u4ec0\u4e48\u8fd9\u6bb5 JavaScript \u4ee3\u7801\u4e2d\u7684 `i` \u59cb\u7ec8\u8f93\u51fa 6\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>js \u95ed\u5305\u95ee\u9898<\/strong><\/p>\n<p>\u7ed9\u5b9a\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;!doctype html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"utf-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n    &lt;meta http-equiv=\"x-ua-compatible\" content=\"ie=edge\"&gt;\n    &lt;title&gt;document&lt;\/title&gt;\n    &lt;style&gt;\n        ul li:nth-child(1) {\n            background: #00ffff;\n        }\n        ul li:nth-child(2) {\n            background: #0011ff;\n        }\n        ul li:nth-child(3) {\n            background: #ff00ff;\n        }\n        ul li:nth-child(4) {\n            background: #aaffff;\n        }\n        ul li:nth-child(5) {\n            background: #ffff00;\n        }\n        ul li:nth-child(6) {\n            background: #00ff00;\n        }\n    &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;input type=\"button\" value=\"a\"&gt;\n    &lt;input type=\"button\" value=\"b\"&gt;\n    &lt;input type=\"button\" value=\"c\"&gt;\n    &lt;input type=\"button\" value=\"d\"&gt;\n    &lt;input type=\"button\" value=\"e\"&gt;\n    &lt;input type=\"button\" value=\"f\"&gt;\n\n    &lt;ul&gt;\n        &lt;li&gt;1&lt;\/li&gt;\n        &lt;li&gt;2&lt;\/li&gt;\n        &lt;li&gt;3&lt;\/li&gt;\n        &lt;li&gt;4&lt;\/li&gt;\n        &lt;li&gt;5&lt;\/li&gt;\n        &lt;li&gt;6&lt;\/li&gt;\n    &lt;\/ul&gt;\n    &lt;script&gt;\n        var oinput = document.getelementsbytagname(\"input\");\n        var oli = document.getelementsbytagname(\"li\");\n        for (var i = 0; i &lt; oinput.length; i++) {\n            oinput[i].index = i;\n            oinput[i].onclick = function() {\n                for (var j = 0; j &lt; oli.length; j++) {\n                    console.log(i); \/\/ i\u8f93\u51fa\u4e3a6\n                    oli[j].style.display = \"block\";\n                }\n                console.log(i); \/\/ i\u8f93\u51fa\u4e3a6\n                oli[this.index].style.display = \"none\";\n            }\n        }\n    &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u95ee\u9898\uff1a<\/p>\n<p>\u6267\u884c\u6b64\u4ee3\u7801\u65f6\uff0c\u4e3a\u4ec0\u4e48\u8f93\u51fa\u7684\u59cb\u7ec8\u662f 6\uff1f<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u7b54\u6848\uff1a<\/p>\n<p>\u8fd9\u662f\u7531\u4e8e javascript \u4e2d\u7684\u95ed\u5305\u6240\u81f4\u3002\u5f53\u4e3a\u6bcf\u4e2a\u8f93\u5165\u6309\u94ae\u8bbe\u7f6e\u5355\u51fb\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u65f6\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u95ed\u5305\uff0c\u8be5\u95ed\u5305\u5305\u542b\u5f53\u524d\u7684 i \u503c\u3002\u5355\u51fb\u4efb\u4f55\u6309\u94ae\u65f6\uff0c\u90fd\u4f1a\u8c03\u7528\u95ed\u5305\uff0c\u5e76\u5c06\u5f53\u524d\u7684 i \u503c\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002\u7136\u800c\uff0c\u7531\u4e8e i \u662f\u95ed\u5305\u5185\u7684\u53d8\u91cf\uff0c\u5f53\u5355\u51fb\u6700\u540e\u4e00\u4e2a\u6309\u94ae\u65f6\uff0c\u5b83\u59cb\u7ec8\u4fdd\u7559\u5176\u6700\u7ec8\u503c\uff0c\u5373 6\u3002<\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u53ef\u4ee5\u5728\u5355\u51fb\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u5185\u4f7f\u7528\u7acb\u5373\u8c03\u7528\u51fd\u6570\u8868\u8fbe\u5f0f (iife) \u6765\u521b\u5efa\u65b0\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u6309\u94ae\u6307\u5b9a\u4e00\u4e2a\u552f\u4e00\u7684 i \u503c\uff1a<\/p>\n<pre>for (var i = 0; i &lt; oInput.length; i++) {\n  oInput[i].index = i;\n  (function(index) {\n    oInput[i].onclick = function() {\n      for (var j = 0; j &lt; oLi.length; j++) {\n        console.log(index);\n        oLi[j].style.display = \"block\";\n      }\n      console.log(index);\n      oLi[index].style.display = \"none\";\n    }\n  })(i);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e3a\u4ec0\u4e48\u8fd9\u6bb5 JavaScript \u4ee3\u7801\u4e2d\u7684 `i` \u59cb\u7ec8\u8f93\u51fa 6\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>js \u95ed\u5305\u95ee\u9898 \u7ed9\u5b9a\u4ee5\u4e0b\u4ee3\u7801\uff1a &lt;!doctype html&gt; &lt;html lang=&#8221;en&#8221;&gt; &lt;head&gt; &lt;meta charset=&#8221;utf-8&#8243;&gt; &lt;meta name=&#8221;viewport&#8221; content=&#8221;width=device-width, initial-scale=1.0&#8243;&gt; &lt;meta http-equiv=&#8221;x-ua-compatible&#8221; content=&#8221;ie=edge&#8221;&gt; &lt;title&gt;document&lt;\/title&gt; &lt;style&gt; ul li:nth-child(1) { background: #00ffff; } ul li:nth-child(2) { background: #0011ff; } ul li:nth-child(3) { background: #ff00ff; } ul li:nth-child(4) { background: #aaffff; } ul li:nth-child(5) { background: #ffff00; } ul li:nth-child(6) { background: #00ff00; } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-2921","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=2921"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2921\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}