{"id":312205,"date":"2024-05-20T22:33:10","date_gmt":"2024-05-20T14:33:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/312205.html"},"modified":"2024-05-20T22:33:25","modified_gmt":"2024-05-20T14:33:25","slug":"jacascript-%e9%a1%b9%e7%9b%ae%e6%80%8e%e4%b9%88%e5%af%b9-dom-%e8%8a%82%e7%82%b9%e8%bf%9b%e8%a1%8c%e8%8e%b7%e5%8f%96-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/312205.html","title":{"rendered":"jacascript \u9879\u76ee\u600e\u4e48\u5bf9 DOM \u8282\u70b9\u8fdb\u884c\u83b7\u53d6"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26232951\/ba69adfe-bf91-4811-bc9c-ed9ab3fe3119.webp\" alt=\"jacascript \u9879\u76ee\u600e\u4e48\u5bf9 DOM \u8282\u70b9\u8fdb\u884c\u83b7\u53d6\" \/><\/p>\n<p><p><strong>JavaScript \u9879\u76ee\u4e2d\u5bf9DOM\u8282\u70b9\u8fdb\u884c\u83b7\u53d6<\/strong>\u7684\u65b9\u6cd5\u4e3b\u8981\u6709 <code>document.getElementById()<\/code>\u3001<code>document.getElementsByTagName()<\/code>\u3001<code>document.getElementsByClassName()<\/code> \u4ee5\u53ca <code>document.querySelector()<\/code> \u548c <code>document.querySelectorAll()<\/code>\u3002<strong>\u8fd9\u4e9b\u65b9\u6cd5\u5141\u8bb8\u5f00\u53d1\u8005\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u9009\u53d6\u9875\u9762\u4e0a\u7684\u5143\u7d20\uff0c\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u4e0e\u4ea4\u4e92<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c<code>document.getElementById()<\/code> \u662f\u6700\u7b80\u5355\u7684\u83b7\u53d6\u7279\u5b9a\u5143\u7d20\u7684\u65b9\u6cd5\uff0c\u53ea\u9700\u63d0\u4f9b\u5143\u7d20\u7684ID\u3002\u5b83\u662f\u9ad8\u6548\u7684\uff0c\u56e0\u4e3aID\u5728\u4e00\u4e2aHTML\u6587\u6863\u4e2d\u662f\u552f\u4e00\u7684\u3002\u6b64\u65b9\u6cd5\u7acb\u5373\u8fd4\u56de\u4e0e\u7279\u5b9aID\u5173\u8054\u7684DOM\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u901a\u8fc7ID\u83b7\u53d6\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u83b7\u53d6\u5177\u6709\u7279\u5b9aID\u7684\u5143\u7d20\u662f\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u4e00\u9879\u64cd\u4f5c\u3002ID\u5728HTML\u6587\u6863\u4e2d\u552f\u4e00\u6807\u8bc6\u4e00\u4e2a\u5143\u7d20\uff0c\u56e0\u6b64\u4f7f\u7528 <code>getElementById<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5feb\u901f\u8bbf\u95ee\u8be5\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var element = document.getElementById(&#039;myElementId&#039;);<\/p>\n<p>\/\/ \u73b0\u5728\u53ef\u4ee5\u5bf9element\u8fdb\u884c\u64cd\u4f5c\uff0c\u5982\u66f4\u6539\u6837\u5f0f\u3001\u6dfb\u52a0\u4e8b\u4ef6\u76d1\u542c\u5668\u7b49\u3002<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e00\u4e2a\u5e38\u89c1\u7684\u7528\u4f8b\u662f\u4fee\u6539\u9009\u5b9a\u5143\u7d20\u7684\u6837\u5f0f\u6216\u5185\u5bb9\u3002 <\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">if (element) {<\/p>\n<p>    element.innerHTML = &#039;\u66f4\u65b0\u7684\u5185\u5bb9&#039;;<\/p>\n<p>    element.style.backgroundColor = &#039;lightblue&#039;;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u786e\u4fddID\u7684\u552f\u4e00\u6027\uff0c\u53ef\u4ee5\u907f\u514d\u5728DOM\u6811\u4e2d\u8bfb\u53d6\u9519\u8bef\u7684\u5143\u7d20\uff0c\u4fdd\u8bc1\u83b7\u53d6\u64cd\u4f5c\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7\u6807\u7b7e\u540d\u83b7\u53d6\u5143\u7d20\u96c6\u5408<\/h3>\n<\/p>\n<p><p>\u82e5\u8981\u9009\u53d6\u5177\u6709\u7279\u5b9a\u6807\u7b7e\u7684\u6240\u6709\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>getElementsByTagName<\/code> \u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u6307\u5b9a\u6807\u7b7e\u540d\u5143\u7d20\u7684HTMLCollection\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var elements = document.getElementsByTagName(&#039;p&#039;);<\/p>\n<p>\/\/ \u73b0\u5728\u53ef\u4ee5\u904d\u5386\u8fd9\u4e9b\u6bb5\u843d\u5143\u7d20\u5e76\u8fdb\u884c\u64cd\u4f5c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u64cd\u4f5c\u53ef\u80fd\u5305\u62ec\u904d\u5386\u6240\u6709\u5143\u7d20\u5e76\u8fdb\u884c\u66f4\u6539\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">for (var i = 0; i &lt; elements.length; i++) {<\/p>\n<p>    elements[i].style.fontWeight = &#039;bold&#039;;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u9875\u9762\u4e0a\u7684\u6240\u6709\u7279\u5b9a\u7c7b\u578b\u5143\u7d20\u6267\u884c\u6279\u91cf\u64cd\u4f5c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u7c7b\u540d\u83b7\u53d6\u5143\u7d20\u96c6\u5408<\/h3>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u9009\u53d6\u5177\u6709\u7279\u5b9a\u7c7b\u7684\u6240\u6709\u5143\u7d20\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>getElementsByClassName<\/code> \u65b9\u6cd5\u3002\u540c\u6837\uff0c\u6b64\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u6307\u5b9a\u7c7b\u540d\u5143\u7d20\u7684HTMLCollection\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var elements = document.getElementsByClassName(&#039;myClassName&#039;);<\/p>\n<p>\/\/ \u5bf9\u8fd9\u4e9b\u5177\u6709\u7c7b\u540d\u7684\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u53ef\u80fd\u9700\u8981\u9690\u85cf\u6240\u6709\u5177\u6709\u67d0\u4e2a\u7c7b\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">for (var i = 0; i &lt; elements.length; i++) {<\/p>\n<p>    elements[i].style.display = &#039;none&#039;;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u83b7\u53d6\u7c7b\u540d\u5143\u7d20\u96c6\u5408\u5728\u5904\u7406\u5177\u6709\u5171\u540c\u6837\u5f0f\u6216\u529f\u80fd\u7279\u6027\u7684\u5143\u7d20\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u901a\u8fc7\u9009\u62e9\u5668\u83b7\u53d6\u5355\u4e2a\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5728\u83b7\u53d6\u5355\u4e2aDOM\u8282\u70b9\u65f6\uff0c<code>querySelector<\/code> \u4ee5\u5176\u5f3a\u5927\u548c\u7075\u6d3b\u6027\u800c\u53d7\u6b22\u8fce\u3002\u8be5\u65b9\u6cd5\u63a5\u53d7\u4efb\u4f55\u6709\u6548\u7684CSS\u9009\u62e9\u5668\uff0c\u4f46\u53ea\u8fd4\u56de\u5339\u914d\u8be5\u9009\u62e9\u5668\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var element = document.querySelector(&#039;.myClassName p#myElementId&#039;);<\/p>\n<p>\/\/ \u73b0\u5728\u53ef\u4ee5\u5bf9\u8fd9\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u94fe\u5f0f\u9009\u62e9\u5668\u53ef\u4ee5\u7f29\u5c0f\u9009\u62e9\u7684\u5143\u7d20\u8303\u56f4\uff0c\u83b7\u53d6\u66f4\u7cbe\u786e\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u901a\u8fc7\u9009\u62e9\u5668\u83b7\u53d6\u5143\u7d20\u96c6\u5408<\/h3>\n<\/p>\n<p><p>\u82e5\u8981\u83b7\u53d6\u7b26\u5408CSS\u9009\u62e9\u5668\u7684\u6240\u6709\u5143\u7d20\uff0c\u5219\u5e94\u4f7f\u7528 <code>querySelectorAll<\/code>\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2aNodeList\u5bf9\u8c61\u5305\u542b\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var elements = document.querySelectorAll(&#039;.myClassName&#039;);<\/p>\n<p>\/\/ \u904d\u5386\u8fd9\u4e9b\u5143\u7d20\u5e76\u8fdb\u884c\u64cd\u4f5c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u540c\u6837\u7684\u65b9\u5f0f\u904d\u5386\u5143\u7d20\u96c6\u5408\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">elements.forEach(function(element) {<\/p>\n<p>    element.style.color = &#039;red&#039;;<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>querySelectorAll<\/code> \u5728\u5904\u7406\u73b0\u4ee3Web\u5e94\u7528\u4e2d\u590d\u6742\u7684DOM\u9009\u62e9\u9700\u6c42\u65f6\u663e\u5f97\u5c24\u5176\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u83b7\u53d6\u5143\u7d20\u7684\u5b50\u8282\u70b9<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u5143\u7d20\u7684\u5b50\u8282\u70b9\u4e5f\u662fDOM\u64cd\u4f5c\u4e2d\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u4f7f\u7528 <code>childNodes<\/code> \u5c5e\u6027\u80fd\u591f\u83b7\u53d6\u5143\u7d20\u7684\u6240\u6709\u5b50\u8282\u70b9\uff0c\u5305\u62ec\u6587\u672c\u8282\u70b9\u548c\u6ce8\u91ca\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var childNodes = element.childNodes;<\/p>\n<p>\/\/ \u904d\u5386\u5b50\u8282\u70b9\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e3a\u4e86\u4ec5\u83b7\u53d6\u5143\u7d20\u7c7b\u578b\u7684\u5b50\u8282\u70b9\uff0c\u901a\u5e38\u4f7f\u7528 <code>children<\/code> \u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var children = element.children;<\/p>\n<p>\/\/ \u8fd9\u5c06\u6392\u9664\u6587\u672c\u8282\u70b9\u548c\u6ce8\u91ca\uff0c\u4f7f\u6211\u4eec\u80fd\u591f\u76f4\u63a5\u5bf9\u5b50\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>childNodes<\/code> \u548c <code>children<\/code> \u5e38\u7528\u4e8e\u5904\u7406\u5d4c\u5957\u5143\u7d20\uff0c\u5e76\u6267\u884c\u5bf9\u8fd9\u4e9b\u5143\u7d20\u7684\u9012\u5f52\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0cJavaScript\u9879\u76ee\u4e2d\u7684DOM\u8282\u70b9\u83b7\u53d6\u53d8\u5f97\u65e2\u76f4\u63a5\u53c8\u7075\u6d3b\u3002\u65e0\u8bba\u662f\u901a\u8fc7ID\u3001\u6807\u7b7e\u540d\u3001\u7c7b\u540d\uff0c\u8fd8\u662f\u5229\u7528\u66f4\u590d\u6742\u7684CSS\u9009\u62e9\u5668\uff0c\u5f00\u53d1\u8005\u90fd\u80fd\u627e\u5230\u5bf9\u5e94\u7684\u9014\u5f84\u6765\u9009\u53d6\u9875\u9762\u4e0a\u7684\u4efb\u4f55\u5143\u7d20\uff0c\u5b9e\u73b0\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u52a8\u6001\u7684\u7528\u6237\u4ea4\u4e92\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u5bf9\u4e8e\u4efb\u4f55\u5e0c\u671b\u7cbe\u7ec6\u64cd\u63a7\u9875\u9762\u5185\u5bb9\u548c\u7ed3\u6784\u7684\u524d\u7aef\u5f00\u53d1\u8005\u800c\u8a00\u90fd\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528 JavaScript \u83b7\u53d6 DOM \u8282\u70b9\uff1f<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u65b9\u6cd5\u548c\u5c5e\u6027\u6765\u83b7\u53d6 DOM \u8282\u70b9\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 <code>document.getElementById()<\/code> \u6765\u6839\u636e\u5143\u7d20\u7684 id \u83b7\u53d6\u8282\u70b9\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>document.querySelector()<\/code> \u548c <code>document.querySelectorAll()<\/code> \u6765\u6839\u636e\u9009\u62e9\u5668\u83b7\u53d6\u8282\u70b9\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u6839\u636e id \u83b7\u53d6\u5355\u4e2a\u8282\u70b9\nvar element = document.getElementById(&quot;myElement&quot;);\n\n\/\/ \u6839\u636e\u9009\u62e9\u5668\u83b7\u53d6\u5355\u4e2a\u8282\u70b9\nvar element = document.querySelector(&quot;.myClass&quot;);\n\n\/\/ \u6839\u636e\u9009\u62e9\u5668\u83b7\u53d6\u591a\u4e2a\u8282\u70b9\nvar elements = document.querySelectorAll(&quot;p&quot;);\n<\/code><\/pre>\n<p><strong>2. \u5982\u4f55\u904d\u5386\u548c\u64cd\u4f5c\u83b7\u53d6\u5230\u7684 DOM \u8282\u70b9\uff1f<\/strong><\/p>\n<p>\u83b7\u53d6\u5230 DOM \u8282\u70b9\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u65b9\u6cd5\u548c\u5c5e\u6027\u6765\u904d\u5386\u548c\u64cd\u4f5c\u8282\u70b9\u3002\u5e38\u89c1\u7684\u64cd\u4f5c\u5305\u62ec\u83b7\u53d6\u8282\u70b9\u7684\u5c5e\u6027\u503c\u3001\u4fee\u6539\u8282\u70b9\u7684\u5185\u5bb9\u3001\u6dfb\u52a0\u6837\u5f0f\u7c7b\u7b49\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u83b7\u53d6\u8282\u70b9\u7684\u5c5e\u6027\u503c\nvar value = element.getAttribute(&quot;src&quot;);\n\n\/\/ \u4fee\u6539\u8282\u70b9\u7684\u5185\u5bb9\nelement.innerHTML = &quot;\u65b0\u7684\u5185\u5bb9&quot;;\n\n\/\/ \u6dfb\u52a0\u6837\u5f0f\u7c7b\nelement.classList.add(&quot;highlight&quot;);\n<\/code><\/pre>\n<p><strong>3. \u5982\u4f55\u5728\u7279\u5b9a\u7684\u7236\u8282\u70b9\u4e0b\u67e5\u627e\u5b50\u8282\u70b9\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\u4ece\u7279\u5b9a\u7684\u7236\u8282\u70b9\u4e0b\u67e5\u627e\u5b50\u8282\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>querySelector()<\/code> \u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u83b7\u53d6\u7236\u8282\u70b9\nvar parent = document.getElementById(&quot;myParent&quot;);\n\n\/\/ \u5728\u7236\u8282\u70b9\u4e0b\u67e5\u627e\u7279\u5b9a\u7684\u5b50\u8282\u70b9\nvar child = parent.querySelector(&quot;.myChild&quot;);\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c<code>querySelector()<\/code> \u65b9\u6cd5\u53ea\u8fd4\u56de\u5339\u914d\u7684\u7b2c\u4e00\u4e2a\u5b50\u8282\u70b9\uff0c\u5982\u679c\u9700\u8981\u83b7\u53d6\u6240\u6709\u5339\u914d\u7684\u5b50\u8282\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>querySelectorAll()<\/code> \u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript \u9879\u76ee\u4e2d\u5bf9DOM\u8282\u70b9\u8fdb\u884c\u83b7\u53d6\u7684\u65b9\u6cd5\u4e3b\u8981\u6709 document.getElementById( [&hellip;]","protected":false},"author":3,"featured_media":312224,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/312205"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=312205"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/312205\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/312224"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=312205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=312205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=312205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}