{"id":167945,"date":"2024-05-08T16:13:25","date_gmt":"2024-05-08T08:13:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/167945.html"},"modified":"2024-05-08T16:13:28","modified_gmt":"2024-05-08T08:13:28","slug":"javascript-%e4%b8%ad%e6%9c%89%e5%93%aa%e4%ba%9b%e5%b8%b8%e8%a7%81%e7%9a%84-dom-%e8%8a%82%e7%82%b9%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/167945.html","title":{"rendered":"JavaScript \u4e2d\u6709\u54ea\u4e9b\u5e38\u89c1\u7684 DOM \u8282\u70b9\u64cd\u4f5c"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27033945\/5f73f24f-f037-4d72-b93a-1a8f4ff368b7.webp\" alt=\"JavaScript \u4e2d\u6709\u54ea\u4e9b\u5e38\u89c1\u7684 DOM \u8282\u70b9\u64cd\u4f5c\" \/><\/p>\n<p><p>\u5728JavaScript\u4e2d\u8fdb\u884cDOM\u8282\u70b9\u64cd\u4f5c\u662f\u4e00\u9879\u57fa\u7840\u4e14\u91cd\u8981\u7684\u6280\u80fd\u3002\u901a\u8fc7\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5<strong>\u52a8\u6001\u521b\u5efa\u3001\u8bbf\u95ee\u3001\u4fee\u6539\u4ee5\u53ca\u5220\u9664\u7f51\u9875\u7684\u5143\u7d20\u548c\u5185\u5bb9<\/strong>\u3002<strong>\u5e38\u89c1\u7684DOM\u8282\u70b9\u64cd\u4f5c<\/strong>\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a\u521b\u5efa\u65b0\u8282\u70b9\u3001\u63d2\u5165\u8282\u70b9\u3001\u5220\u9664\u8282\u70b9\u3001\u66ff\u6362\u8282\u70b9\u3001\u590d\u5236\u8282\u70b9\uff0c\u4ee5\u53ca\u83b7\u53d6\u548c\u4fee\u6539\u8282\u70b9\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u521b\u5efa\u65b0\u8282\u70b9<\/strong>\u662f\u6700\u57fa\u7840\u4e5f\u662f\u6700\u9891\u7e41\u4f7f\u7528\u7684DOM\u64cd\u4f5c\u4e4b\u4e00\u3002\u8fd9\u901a\u5e38\u901a\u8fc7<code>document.createElement()<\/code>\u65b9\u6cd5\u5b9e\u73b0\u3002\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u6807\u7b7e\u540d\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u521b\u5efa\u7684\u5143\u7d20\uff0c\u8be5\u5143\u7d20\u5c1a\u672a\u6dfb\u52a0\u5230\u6587\u6863\u4e2d\u3002\u521b\u5efa\u8282\u70b9\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>appendChild()<\/code>\u6216<code>insertBefore()<\/code>\u65b9\u6cd5\u5c06\u5176\u63d2\u5165\u5230\u6587\u6863\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efa\u65b0\u8282\u70b9<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u65b0\u7684DOM\u8282\u70b9\u901a\u5e38\u662f\u4e3a\u4e86\u52a8\u6001\u6dfb\u52a0\u5185\u5bb9\u5230\u7f51\u9875\u4e2d\u3002\u8fd9\u6837\u7684\u64cd\u4f5c\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u4e0d\u5237\u65b0\u9875\u9762\u7684\u60c5\u51b5\u4e0b\uff0c\u66f4\u65b0\u9875\u9762\u7684\u5185\u5bb9\u6216\u6837\u5f0f\u3002\u4f7f\u7528<code>document.createElement(tagName)<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5143\u7d20\u8282\u70b9\u3002<code>tagName<\/code>\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u5e0c\u671b\u521b\u5efa\u7684\u5143\u7d20\u7684\u6807\u7b7e\u540d\uff0c\u6bd4\u5982<code>&#039;div&#039;<\/code>\u3001<code>&#039;span&#039;<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u5b8c\u8282\u70b9\u4e4b\u540e\uff0c\u4f60\u53ef\u80fd\u8fd8\u9700\u8981\u7ed9\u5b83\u8bbe\u7f6e\u4e00\u4e9b\u5c5e\u6027\u6216\u5185\u5bb9\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u64cd\u4f5c\u8fd4\u56de\u7684\u5143\u7d20\u5bf9\u8c61\u6765\u5b9e\u73b0\u3002\u6bd4\u5982\u4f7f\u7528<code>.innerText<\/code>\u6216<code>.innerHTML<\/code>\u5c5e\u6027\u8bbe\u7f6e\u5143\u7d20\u7684\u6587\u672c\u6216HTML\u5185\u5bb9\uff0c\u6216\u8005\u4f7f\u7528<code>.setAttribute(name, value)<\/code>\u65b9\u6cd5\u7ed9\u5143\u7d20\u6dfb\u52a0\u65b0\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u63d2\u5165\u8282\u70b9<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u521b\u5efa\u4e86\u65b0\u7684DOM\u5143\u7d20\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u5c06\u8fd9\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230\u6587\u6863\u4e2d\u7684\u67d0\u4e2a\u73b0\u6709\u5143\u7d20\u4e0b\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7<code>appendChild()<\/code>\u548c<code>insertBefore()<\/code>\u65b9\u6cd5\u5b8c\u6210\u3002<code>appendChild()<\/code>\u65b9\u6cd5\u5c06\u4e00\u4e2a\u8282\u70b9\u6dfb\u52a0\u5230\u6307\u5b9a\u7236\u8282\u70b9\u7684\u5b50\u8282\u70b9\u5217\u8868\u7684\u672b\u5c3e\u3002\u800c<code>insertBefore()<\/code>\u65b9\u6cd5\u5219\u5c06\u8282\u70b9\u63d2\u5165\u5230\u7236\u8282\u70b9\u7684\u7279\u5b9a\u5b50\u8282\u70b9\u4e4b\u524d\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u60f3\u8981\u5728\u9875\u9762\u4e0a\u663e\u793a\u65b0\u521b\u5efa\u7684\u5143\u7d20\uff0c\u4f60\u9700\u8981\u627e\u5230\u4e00\u4e2a\u73b0\u6709\u7684\u7236\u5143\u7d20\uff0c\u7136\u540e\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u4e4b\u4e00\u8fdb\u884c\u63d2\u5165\u3002\u7236\u5143\u7d20\u53ef\u4ee5\u662f\u4efb\u4f55\u80fd\u591f\u5305\u542b\u5176\u4ed6\u5143\u7d20\u7684DOM\u8282\u70b9\uff0c\u5982<code>&lt;div&gt;<\/code>\u3001<code>&lt;body&gt;<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5220\u9664\u8282\u70b9<\/h3>\n<\/p>\n<p><p>\u5728\u7f51\u9875\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u5220\u9664\u9875\u9762\u4e0a\u7684\u67d0\u4e9b\u5143\u7d20\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7<code>removeChild()<\/code>\u65b9\u6cd5\u5b8c\u6210\u3002\u8be5\u65b9\u6cd5\u4eceDOM\u4e2d\u5220\u9664\u4e00\u4e2a\u5b50\u8282\u70b9\uff0c\u91ca\u653e\u4e0e\u4e4b\u76f8\u5173\u7684\u8d44\u6e90\u3002\u4f7f\u7528\u8be5\u65b9\u6cd5\u65f6\uff0c\u4f60\u9700\u8981\u9996\u5148\u83b7\u53d6\u6b32\u5220\u9664\u8282\u70b9\u7684\u7236\u8282\u70b9\uff0c\u7136\u540e\u8c03\u7528<code>parentNode.removeChild(childNode)<\/code>\uff0c\u5176\u4e2d<code>childNode<\/code>\u662f\u9700\u8981\u5220\u9664\u7684\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u5220\u9664\u8282\u70b9\u662f\u4e00\u4e2a\u654f\u611f\u7684\u64cd\u4f5c\uff0c\u9700\u8981\u786e\u4fdd\u4e0d\u4f1a\u8bef\u5220\u9664\u672a\u5907\u4efd\u7684\u91cd\u8981\u5185\u5bb9\uff0c\u540c\u65f6\u8003\u8651\u5230\u9875\u9762\u5e03\u5c40\u7684\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u66ff\u6362\u8282\u70b9<\/h3>\n<\/p>\n<p><p>DOM\u63d0\u4f9b\u4e86<code>replaceChild(newNode, oldNode)<\/code>\u65b9\u6cd5\uff0c\u5141\u8bb8\u4f60\u7528\u4e00\u4e2a\u65b0\u7684\u8282\u70b9\u66ff\u6362\u73b0\u6709\u7684\u4e00\u4e2a\u8282\u70b9\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9700\u8981\u5728\u7236\u8282\u70b9\u4e0a\u8c03\u7528\uff0c\u5e76\u5c06\u65b0\u8282\u70b9\u548c\u65e7\u8282\u70b9\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002\u8fd9\u79cd\u64cd\u4f5c\u5728\u9700\u8981\u66f4\u65b0\u9875\u9762\u67d0\u90e8\u5206\u5185\u5bb9\u65f6\u975e\u5e38\u6709\u7528\uff0c\u5b83\u5141\u8bb8\u4f60\u4fdd\u6301\u5176\u4ed6DOM\u7ed3\u6784\u4e0d\u53d8\uff0c\u53ea\u66ff\u6362\u7279\u5b9a\u7684\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u66ff\u6362\u64cd\u4f5c\u5f80\u5f80\u9700\u8981\u7cbe\u786e\u63a7\u5236\uff0c\u56e0\u4e3a\u5b83\u6d89\u53ca\u5230\u5c06\u73b0\u6709\u7684\u5143\u7d20\u5b8c\u5168\u66ff\u6362\u4e3a\u65b0\u5143\u7d20\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bf9\u4e8b\u4ef6\u76d1\u542c\u548c\u5143\u7d20\u6837\u5f0f\u4ea7\u751f\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u590d\u5236\u8282\u70b9<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u8282\u70b9\u7684\u526f\u672c\u800c\u4e0d\u662f\u5b8c\u5168\u65b0\u7684\u5143\u7d20\u3002DOM\u63d0\u4f9b\u4e86<code>cloneNode(deep)<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u8fd9\u4e2a\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u8c03\u7528\u5b83\u7684\u8282\u70b9\u7684\u7cbe\u786e\u526f\u672c\uff0c\u5e76\u53ef\u4ee5\u9009\u62e9\u662f\u5426\u8fdb\u884c\u6df1\u590d\u5236\u3002\u53c2\u6570<code>deep<\/code>\u4e3a<code>true<\/code>\u65f6\uff0c\u4f1a\u8fde\u540c\u8282\u70b9\u4e0b\u7684\u6240\u6709\u5b50\u5b59\u8282\u70b9\u4e00\u8d77\u590d\u5236\uff0c\u4e3a<code>false<\/code>\u65f6\uff0c\u5219\u53ea\u590d\u5236\u8be5\u8282\u70b9\u672c\u8eab\u3002<\/p>\n<\/p>\n<p><p>\u590d\u5236\u8282\u70b9\u901a\u5e38\u7528\u4e8e\u9700\u8981\u5927\u91cf\u76f8\u4f3cDOM\u7ed3\u6784\u7684\u573a\u666f\uff0c\u901a\u8fc7\u590d\u5236\u57fa\u7840\u7ed3\u6784\u518d\u8fdb\u884c\u5c0f\u89c4\u6a21\u4fee\u6539\uff0c\u53ef\u4ee5\u5927\u5927\u51cf\u5c11\u4ee3\u7801\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u83b7\u53d6\u548c\u4fee\u6539\u8282\u70b9\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u6bcf\u4e2aDOM\u8282\u70b9\u90fd\u6709\u4e00\u7cfb\u5217\u5c5e\u6027\uff0c\u5982<code>id<\/code>\u3001<code>class<\/code>\u3001<code>style<\/code>\u7b49\uff0c\u8fd9\u4e9b\u5c5e\u6027\u53ef\u4ee5\u901a\u8fc7JavaScript\u8fdb\u884c\u8bfb\u53d6\u548c\u4fee\u6539\u3002\u4f7f\u7528<code>getAttribute(attrName)<\/code>\u548c<code>setAttribute(attrName, attrValue)<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5206\u522b\u83b7\u53d6\u548c\u8bbe\u7f6e\u8282\u70b9\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u9488\u5bf9\u4e00\u4e9b\u5e38\u7528\u5c5e\u6027\uff0c\u5982<code>id<\/code>\u548c<code>className<\/code>\uff0cDOM\u8282\u70b9\u8fd8\u63d0\u4f9b\u4e86\u76f4\u63a5\u7684\u5c5e\u6027\u8bbf\u95ee\u65b9\u5f0f\uff0c\u4f8b\u5982<code>element.id<\/code>\u6216<code>element.className<\/code>\u3002\u8fd9\u8ba9\u5c5e\u6027\u7684\u4fee\u6539\u53d8\u5f97\u66f4\u52a0\u65b9\u4fbf\u5feb\u6377\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u5e38\u89c1\u7684DOM\u8282\u70b9\u64cd\u4f5c\uff0c\u5f00\u53d1\u8005\u80fd\u591f\u6709\u6548\u5730\u63a7\u5236\u548c\u64cd\u7eb5\u7f51\u9875\u5185\u5bb9\uff0c\u4e3a\u7528\u6237\u63d0\u4f9b\u4e30\u5bcc\u7684\u4ea4\u4e92\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728JavaScript\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684DOM\u8282\u70b9\uff1f<\/strong><\/p>\n<p>\u8981\u5728JavaScript\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684DOM\u8282\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528createElement\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u8981\u521b\u5efa\u7684\u8282\u70b9\u7c7b\u578b\uff0c\u4f8b\u5982 &quot;div&quot;\u3001&quot;p&quot;\u3001&quot;span&quot;\u7b49\u3002\u521b\u5efa\u65b0\u8282\u70b9\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528appendChild\u65b9\u6cd5\u5c06\u5176\u6dfb\u52a0\u5230\u5df2\u6709\u7684\u8282\u70b9\u4e2d\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728JavaScript\u4e2d\u5220\u9664\u4e00\u4e2aDOM\u8282\u70b9\uff1f<\/strong><\/p>\n<p>\u8981\u5220\u9664\u4e00\u4e2aDOM\u8282\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528removeChild\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9700\u8981\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u8981\u5220\u9664\u7684\u8282\u70b9\u5bf9\u8c61\u3002\u9996\u5148\uff0c\u9700\u8981\u627e\u5230\u8981\u5220\u9664\u7684\u8282\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528querySelector\u6216getElementById\u7b49\u65b9\u6cd5\u83b7\u53d6\u5230\u8282\u70b9\u5bf9\u8c61\uff0c\u7136\u540e\u518d\u8c03\u7528removeChild\u65b9\u6cd5\u5c06\u5176\u5220\u9664\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728JavaScript\u4e2d\u4fee\u6539\u4e00\u4e2aDOM\u8282\u70b9\u7684\u6587\u672c\u5185\u5bb9\uff1f<\/strong><\/p>\n<p>\u8981\u4fee\u6539\u4e00\u4e2aDOM\u8282\u70b9\u7684\u6587\u672c\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528textContent\u5c5e\u6027\u6216innerText\u5c5e\u6027\u3002\u8fd9\u4e24\u4e2a\u5c5e\u6027\u90fd\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u6216\u8bbe\u7f6e\u8282\u70b9\u7684\u6587\u672c\u5185\u5bb9\u3002textContent\u5c5e\u6027\u4f1a\u5c06\u8282\u70b9\u4e2d\u7684\u6240\u6709\u6587\u672c\u5185\u5bb9\u4f5c\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u8fd4\u56de\uff0c\u800cinnerText\u5c5e\u6027\u53ea\u8fd4\u56de\u53ef\u89c1\u7684\u6587\u672c\u5185\u5bb9\uff0c\u4f1a\u5ffd\u7565\u6389\u88abCSS\u6837\u5f0f\u9690\u85cf\u7684\u6587\u672c\u3002\u53ef\u4ee5\u901a\u8fc7\u7ed9\u8fd9\u4e24\u4e2a\u5c5e\u6027\u8d4b\u503c\u6765\u4fee\u6539\u8282\u70b9\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u4e2d\u8fdb\u884cDOM\u8282\u70b9\u64cd\u4f5c\u662f\u4e00\u9879\u57fa\u7840\u4e14\u91cd\u8981\u7684\u6280\u80fd\u3002\u901a\u8fc7\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u52a8\u6001\u521b\u5efa\u3001\u8bbf\u95ee\u3001\u4fee\u6539 [&hellip;]","protected":false},"author":3,"featured_media":167949,"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\/167945"}],"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=167945"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/167945\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/167949"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=167945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=167945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=167945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}