{"id":2116,"date":"2025-02-27T23:53:41","date_gmt":"2025-02-27T15:53:41","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2116"},"modified":"2025-02-27T23:53:41","modified_gmt":"2025-02-27T15:53:41","slug":"javascript-html-dom-%e8%8a%82%e7%82%b9%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/27\/javascript-html-dom-%e8%8a%82%e7%82%b9%e5%88%97%e8%a1%a8\/","title":{"rendered":"JavaScript HTML DOM \u8282\u70b9\u5217\u8868"},"content":{"rendered":"\n<p>\u5728 JavaScript \u4e2d\uff0c\u8282\u70b9\u5217\u8868\uff08NodeList\uff09\u662f\u4e00\u4e2a\u7c7b\u6570\u7ec4\u5bf9\u8c61\uff0c\u5305\u542b\u4e86\u4e0e\u67e5\u8be2\u6761\u4ef6\u5339\u914d\u7684\u6240\u6709\u8282\u70b9\u3002\u5b83\u53ef\u4ee5\u7531\u591a\u79cd\u65b9\u6cd5\u8fd4\u56de\uff0c\u4f8b\u5982 <code>getElementsByTagName()<\/code>\u3001<code>querySelectorAll()<\/code>\u3001<code>childNodes<\/code> \u7b49\u3002\u8282\u70b9\u5217\u8868\u7528\u4e8e\u8868\u793a DOM \u6811\u4e2d\u7684\u8282\u70b9\u96c6\u5408\uff0c\u901a\u5e38\u5305\u542b\u591a\u4e2a\u5143\u7d20\u8282\u70b9\u3001\u6587\u672c\u8282\u70b9\u6216\u5176\u4ed6\u7c7b\u578b\u7684\u8282\u70b9\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. <code>NodeList<\/code> \u6982\u8ff0<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>NodeList<\/strong> \u662f\u4e00\u4e2a\u7c7b\u6570\u7ec4\u5bf9\u8c61\uff0c\u4ee3\u8868 DOM \u6811\u4e2d\u7684\u4e00\u7ec4\u8282\u70b9\u3002\u5b83\u5e76\u4e0d\u50cf\u6570\u7ec4\u90a3\u6837\u76f4\u63a5\u5177\u5907\u6570\u7ec4\u7684\u6240\u6709\u65b9\u6cd5\uff08\u5982 <code>map<\/code>\u3001<code>reduce<\/code> \u7b49\uff09\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528 <code>length<\/code> \u5c5e\u6027\u6765\u83b7\u53d6\u8282\u70b9\u7684\u6570\u91cf\uff0c\u5e76\u4f7f\u7528 <code>item(index)<\/code> \u65b9\u6cd5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u8282\u70b9\u3002<\/li>\n\n\n\n<li><code>NodeList<\/code> \u53ef\u4ee5\u662f\u52a8\u6001\u7684\u6216\u9759\u6001\u7684\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u8fd4\u56de\u5b83\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. <code>NodeList<\/code> \u7684\u5e38\u89c1\u6765\u6e90<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><code>childNodes<\/code> \u5c5e\u6027<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>childNodes<\/code> \u8fd4\u56de\u4e00\u4e2a <strong>NodeList<\/strong>\uff0c\u5305\u542b\u4e86\u5f53\u524d\u8282\u70b9\u7684\u6240\u6709\u5b50\u8282\u70b9\uff08\u5305\u62ec\u5143\u7d20\u8282\u70b9\u3001\u6587\u672c\u8282\u70b9\u548c\u6ce8\u91ca\u8282\u70b9\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><code>querySelectorAll()<\/code> \u65b9\u6cd5<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>querySelectorAll()<\/code> \u8fd4\u56de\u4e00\u4e2a\u9759\u6001\u7684 <strong>NodeList<\/strong>\uff0c\u5b83\u5305\u542b\u4e86\u6240\u6709\u4e0e\u6307\u5b9a CSS \u9009\u62e9\u5668\u5339\u914d\u7684\u8282\u70b9\u3002\u5b83\u4e0d\u4f1a\u81ea\u52a8\u66f4\u65b0\uff0c\u5f53 DOM \u6811\u53d8\u5316\u65f6\uff0c\u8fd4\u56de\u7684 <code>NodeList<\/code> \u4e0d\u4f1a\u53d1\u751f\u53d8\u5316\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><code>getElementsByTagName()<\/code> \u65b9\u6cd5<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>getElementsByTagName()<\/code> \u8fd4\u56de\u4e00\u4e2a\u52a8\u6001\u7684 <strong>NodeList<\/strong>\uff0c\u5305\u542b\u6587\u6863\u6216\u67d0\u4e2a\u5143\u7d20\u5185\u7684\u6240\u6709\u5177\u6709\u6307\u5b9a\u6807\u7b7e\u540d\u7684\u5b50\u5143\u7d20\u3002\u5b83\u662f\u201c\u6d3b\u7684\u201d\uff0c\u5373 DOM \u53d1\u751f\u53d8\u5316\u65f6\uff0c<code>NodeList<\/code> \u4f1a\u81ea\u52a8\u66f4\u65b0\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><code>getElementsByClassName()<\/code> \u65b9\u6cd5<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>getElementsByClassName()<\/code> \u8fd4\u56de\u4e00\u4e2a\u52a8\u6001\u7684 <strong>NodeList<\/strong>\uff0c\u5305\u542b\u6240\u6709\u5177\u6709\u6307\u5b9a\u7c7b\u540d\u7684\u5143\u7d20\u8282\u70b9\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. <code>NodeList<\/code> \u4e0e <code>HTMLCollection<\/code> \u7684\u533a\u522b<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>HTMLCollection<\/code><\/strong> \u662f\u4e00\u4e2a\u201c\u6d3b\u7684\u201d\u96c6\u5408\uff0c\u5b83\u4f1a\u968f\u7740 DOM \u7684\u53d8\u5316\u800c\u81ea\u52a8\u66f4\u65b0\u3002\u8fd4\u56de\u8be5\u96c6\u5408\u7684\u65b9\u6cd5\u5305\u62ec <code>getElementsByClassName()<\/code> \u548c <code>getElementsByTagName()<\/code>\u3002<\/li>\n\n\n\n<li><strong><code>NodeList<\/code><\/strong> \u662f\u4e00\u4e2a\u7c7b\u6570\u7ec4\u7684\u96c6\u5408\uff0c\u901a\u5e38\u901a\u8fc7 <code>querySelectorAll()<\/code> \u7b49\u65b9\u6cd5\u8fd4\u56de\u3002\u5b83\u53ef\u4ee5\u662f\u9759\u6001\u7684\uff0c\u4e5f\u53ef\u4ee5\u662f\u52a8\u6001\u7684\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u8fd4\u56de\u5b83\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. <code>NodeList<\/code> \u5c5e\u6027\u4e0e\u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>length<\/code><\/strong>\uff1a\u8fd4\u56de\u8282\u70b9\u5217\u8868\u4e2d\u7684\u8282\u70b9\u6570\u3002<\/li>\n\n\n\n<li><strong><code>item(index)<\/code><\/strong>\uff1a\u8fd4\u56de\u6307\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u7684\u8282\u70b9\u3002<\/li>\n\n\n\n<li><strong><code>forEach()<\/code><\/strong>\uff1a\u5bf9\u4e8e\u7531 <code>querySelectorAll()<\/code> \u8fd4\u56de\u7684 <code>NodeList<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>forEach()<\/code> \u65b9\u6cd5\u6765\u904d\u5386\u6bcf\u4e2a\u8282\u70b9\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 <code>NodeList<\/code><\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div id=\"parent\"&gt;\n  &lt;p class=\"child\"&gt;Child 1&lt;\/p&gt;\n  &lt;p class=\"child\"&gt;Child 2&lt;\/p&gt;\n  &lt;p class=\"child\"&gt;Child 3&lt;\/p&gt;\n&lt;\/div&gt;\n\n&lt;script&gt;\n  const parent = document.getElementById(\"parent\");\n\n  \/\/ \u83b7\u53d6\u6240\u6709\u5b50\u8282\u70b9\uff08\u5305\u62ec\u6587\u672c\u8282\u70b9\u3001\u5143\u7d20\u8282\u70b9\u7b49\uff09\n  const nodeList = parent.childNodes;\n\n  console.log(nodeList.length);  \/\/ \u8f93\u51fa 5\uff08\u5305\u542b\u6587\u672c\u8282\u70b9\u548c\u5143\u7d20\u8282\u70b9\uff09\n\n  \/\/ \u4f7f\u7528 item() \u83b7\u53d6\u6307\u5b9a\u7d22\u5f15\u7684\u8282\u70b9\n  console.log(nodeList.item(0)); \/\/ \u8f93\u51fa\u7236\u5143\u7d20\u4e4b\u524d\u7684\u7a7a\u767d\u6587\u672c\u8282\u70b9\n  console.log(nodeList.item(1)); \/\/ \u8f93\u51fa\u7b2c\u4e00\u4e2a &lt;p&gt; \u5143\u7d20\u8282\u70b9\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 <code>querySelectorAll()<\/code> \u83b7\u53d6 <code>NodeList<\/code><\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"item\"&gt;Item 1&lt;\/div&gt;\n&lt;div class=\"item\"&gt;Item 2&lt;\/div&gt;\n&lt;div class=\"item\"&gt;Item 3&lt;\/div&gt;\n\n&lt;script&gt;\n  const items = document.querySelectorAll(\".item\"); \/\/ \u8fd4\u56de\u4e00\u4e2a NodeList\n\n  console.log(items.length);  \/\/ \u8f93\u51fa 3\n\n  \/\/ \u4f7f\u7528 forEach \u904d\u5386 NodeList\n  items.forEach(function(item) {\n    console.log(item.innerHTML); \/\/ \u8f93\u51fa \"Item 1\", \"Item 2\", \"Item 3\"\n  });\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u52a8\u6001\u4e0e\u9759\u6001\u7684 <code>NodeList<\/code><\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u52a8\u6001 NodeList<\/strong>\uff1a\u4f8b\u5982\u901a\u8fc7 <code>getElementsByTagName()<\/code> \u6216 <code>getElementsByClassName()<\/code> \u8fd4\u56de\u7684\u8282\u70b9\u5217\u8868\u3002\u5982\u679c DOM \u53d1\u751f\u66f4\u6539\uff08\u5982\u6dfb\u52a0\u6216\u5220\u9664\u8282\u70b9\uff09\uff0c\u8fd9\u4e9b\u8282\u70b9\u5217\u8868\u4f1a\u7acb\u5373\u66f4\u65b0\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  &lt;div class=\"item\"&gt;Item 1&lt;\/div&gt;\n  &lt;div class=\"item\"&gt;Item 2&lt;\/div&gt;\n  &lt;div class=\"item\"&gt;Item 3&lt;\/div&gt;\n\n  &lt;script&gt;\n    const items = document.getElementsByClassName(\"item\");\n\n    \/\/ \u52a8\u6001\u66f4\u65b0 NodeList\n    console.log(items.length);  \/\/ \u8f93\u51fa 3\n\n    \/\/ \u52a8\u6001\u66f4\u6539 DOM\n    const newItem = document.createElement(\"div\");\n    newItem.className = \"item\";\n    newItem.textContent = \"Item 4\";\n    document.body.appendChild(newItem);\n\n    console.log(items.length);  \/\/ \u8f93\u51fa 4\uff08NodeList \u52a8\u6001\u66f4\u65b0\uff09\n  &lt;\/script&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9759\u6001 NodeList<\/strong>\uff1a\u4f8b\u5982\u901a\u8fc7 <code>querySelectorAll()<\/code> \u8fd4\u56de\u7684\u8282\u70b9\u5217\u8868\u3002\u5b83\u4e0d\u4f1a\u968f\u7740 DOM \u66f4\u6539\u800c\u81ea\u52a8\u66f4\u65b0\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  &lt;div class=\"item\"&gt;Item 1&lt;\/div&gt;\n  &lt;div class=\"item\"&gt;Item 2&lt;\/div&gt;\n\n  &lt;script&gt;\n    const items = document.querySelectorAll(\".item\");\n\n    console.log(items.length);  \/\/ \u8f93\u51fa 2\n\n    \/\/ \u52a8\u6001\u66f4\u6539 DOM\n    const newItem = document.createElement(\"div\");\n    newItem.className = \"item\";\n    newItem.textContent = \"Item 3\";\n    document.body.appendChild(newItem);\n\n    console.log(items.length);  \/\/ \u4ecd\u7136\u8f93\u51fa 2\uff08NodeList \u4e0d\u66f4\u65b0\uff09\n  &lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. \u904d\u5386 NodeList<\/strong><\/h3>\n\n\n\n<p>\u7531\u4e8e <code>NodeList<\/code> \u662f\u7c7b\u6570\u7ec4\u7684\u5bf9\u8c61\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u7528\u5faa\u73af\u6216 <code>forEach()<\/code> \u6765\u904d\u5386\u8282\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u904d\u5386 <code>NodeList<\/code>\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  const nodeList = document.querySelectorAll(\".item\");\n  for (let i = 0; i &lt; nodeList.length; i++) {\n    console.log(nodeList&#91;i].innerHTML);\n  }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>forEach()<\/code> \u904d\u5386 <code>NodeList<\/code>\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  const nodeList = document.querySelectorAll(\".item\");\n  nodeList.forEach(function(item) {\n    console.log(item.innerHTML);\n  });<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. \u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>NodeList<\/strong> \u662f\u4e00\u4e2a\u7c7b\u6570\u7ec4\u5bf9\u8c61\uff0c\u901a\u5e38\u901a\u8fc7 DOM \u67e5\u8be2\u65b9\u6cd5\uff08\u5982 <code>querySelectorAll()<\/code>\u3001<code>childNodes<\/code> \u7b49\uff09\u83b7\u53d6\u3002<\/li>\n\n\n\n<li>\u5b83\u53ef\u4ee5\u662f\u9759\u6001\u7684\uff08\u5982 <code>querySelectorAll()<\/code>\uff09\u6216\u52a8\u6001\u7684\uff08\u5982 <code>getElementsByTagName()<\/code>\uff09\u3002<\/li>\n\n\n\n<li><code>NodeList<\/code> \u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u5c5e\u6027\uff08\u5982 <code>length<\/code>\uff09\u548c\u65b9\u6cd5\uff08\u5982 <code>item()<\/code> \u548c <code>forEach()<\/code>\uff09\u6765\u64cd\u4f5c\u8282\u70b9\u96c6\u5408\u3002<\/li>\n\n\n\n<li><code>NodeList<\/code> \u662f\u904d\u5386 DOM \u8282\u70b9\u7684\u91cd\u8981\u5de5\u5177\uff0c\u53ef\u4ee5\u4e0e\u6570\u7ec4\u7c7b\u4f3c\u7684\u65b9\u5f0f\u64cd\u4f5c DOM \u4e2d\u7684\u5143\u7d20\u6216\u8282\u70b9\u96c6\u5408\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7\u7406\u89e3\u548c\u638c\u63e1 <code>NodeList<\/code>\uff0c\u4f60\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u548c\u64cd\u4f5c HTML \u9875\u9762\u4e2d\u7684\u591a\u4e2a\u8282\u70b9\u3002<\/p>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 JavaScript \u4e2d\uff0c\u8282\u70b9\u5217\u8868\uff08NodeList\uff09\u662f\u4e00\u4e2a\u7c7b\u6570\u7ec4\u5bf9\u8c61\uff0c\u5305\u542b\u4e86\u4e0e\u67e5\u8be2\u6761\u4ef6\u5339\u914d\u7684\u6240\u6709\u8282\u70b9\u3002\u5b83 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2116","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2116"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2116\/revisions"}],"predecessor-version":[{"id":2117,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2116\/revisions\/2117"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}