{"id":3493,"date":"2024-11-10T11:53:40","date_gmt":"2024-11-10T03:53:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3493\/"},"modified":"2024-11-10T11:53:40","modified_gmt":"2024-11-10T03:53:40","slug":"js%e5%a6%82%e4%bd%95%e5%a4%8d%e5%88%b6div","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3493\/","title":{"rendered":"js\u5982\u4f55\u590d\u5236div"},"content":{"rendered":"<blockquote><p>\n  \u662f\u7684\uff0c\u5728 javascript \u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u590d\u5236 div\uff1a\u76f4\u63a5\u590d\u5236\uff1a\u4f7f\u7528 clonenode() \u65b9\u6cd5\u3002\u5e26\u6570\u636e\u590d\u5236\uff1a\u4f7f\u7528 innerhtml \u5c5e\u6027\u3002\u9ad8\u7ea7\u65b9\u6cd5\uff1a\u4f7f\u7528 documentfragment \u6216 shadow dom\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110813401822785.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u590d\u5236div\u63d2\u56fe\" alt=\"js\u5982\u4f55\u590d\u5236div\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u590d\u5236 DIV\uff1f<\/strong><\/p>\n<p><strong>\u76f4\u63a5\u590d\u5236\uff1a<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 cloneNode() \u65b9\u6cd5\u76f4\u63a5\u590d\u5236\u4e00\u4e2a DIV\u3002<\/p>\n<pre>\/\/ \u83b7\u53d6\u8981\u590d\u5236\u7684 DIV\nconst originalDiv = document.getElementById('myDiv');\n\n\/\/ \u521b\u5efa\u4e00\u4e2a\u539f DIV \u7684\u526f\u672c\nconst clonedDiv = originalDiv.cloneNode(true);\n\n\/\/ \u5c06\u526f\u672c\u6dfb\u52a0\u5230\u6587\u6863\u4e2d\ndocument.body.appendChild(clonedDiv);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5e26\u6570\u636e\u590d\u5236\uff1a<\/strong><\/p>\n<p>\u5982\u679c\u60a8\u9700\u8981\u590d\u5236 DIV \u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 innerHTML \u5c5e\u6027\u3002<\/p>\n<pre>\/\/ \u83b7\u53d6\u8981\u590d\u5236\u7684 DIV\nconst originalDiv = document.getElementById('myDiv');\n\n\/\/ \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 DIV\nconst clonedDiv = document.createElement('div');\n\n\/\/ \u5c06\u539f DIV \u7684 HTML \u5185\u5bb9\u590d\u5236\u5230\u65b0 DIV \u4e2d\nclonedDiv.innerHTML = originalDiv.innerHTML;\n\n\/\/ \u5c06\u65b0 DIV \u6dfb\u52a0\u5230\u6587\u6863\u4e2d\ndocument.body.appendChild(clonedDiv);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u9ad8\u7ea7\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u66f4\u9ad8\u7ea7\u7684\u65b9\u6cd5\u6765\u590d\u5236 DIV\uff0c\u4f8b\u5982 DocumentFragment \u6216 Shadow DOM\u3002<\/p>\n<ul>\n<li><strong>DocumentFragment\uff1a<\/strong><\/li>\n<\/ul>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a DocumentFragment\nconst fragment = document.createDocumentFragment();\n\n\/\/ \u5c06\u539f DIV \u6dfb\u52a0\u5230 DocumentFragment \u4e2d\nfragment.appendChild(originalDiv.cloneNode(true));\n\n\/\/ \u5c06 DocumentFragment \u6dfb\u52a0\u5230\u6587\u6863\u4e2d\ndocument.body.appendChild(fragment);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li><strong>Shadow DOM\uff1a<\/strong><\/li>\n<\/ul>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a Shadow Root\nconst shadowRoot = originalDiv.createShadowRoot();\n\n\/\/ \u5c06\u539f DIV \u590d\u5236\u5230 Shadow Root \u4e2d\nshadowRoot.appendChild(originalDiv.cloneNode(true));<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u590d\u5236div\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>\u662f\u7684\uff0c\u5728 javascript \u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u590d\u5236 div\uff1a\u76f4\u63a5\u590d\u5236\uff1a\u4f7f\u7528 clonenode() \u65b9\u6cd5\u3002\u5e26\u6570\u636e\u590d\u5236\uff1a\u4f7f\u7528 innerhtml \u5c5e\u6027\u3002\u9ad8\u7ea7\u65b9\u6cd5\uff1a\u4f7f\u7528 documentfragment \u6216 shadow dom\u3002 \u5982\u4f55\u5728 JavaScript \u4e2d\u590d\u5236 DIV\uff1f \u76f4\u63a5\u590d\u5236\uff1a \u60a8\u53ef\u4ee5\u4f7f\u7528 cloneNode() \u65b9\u6cd5\u76f4\u63a5\u590d\u5236\u4e00\u4e2a DIV\u3002 \/\/ \u83b7\u53d6\u8981\u590d\u5236\u7684 DIV const originalDiv = document.getElementById(&#8216;myDiv&#8217;); \/\/ \u521b\u5efa\u4e00\u4e2a\u539f DIV \u7684\u526f\u672c const clonedDiv = originalDiv.cloneNode(true); \/\/ \u5c06\u526f\u672c\u6dfb\u52a0\u5230\u6587\u6863\u4e2d document.body.appendChild(clonedDiv); \u767b\u5f55\u540e\u590d\u5236 \u5e26\u6570\u636e\u590d\u5236\uff1a \u5982\u679c\u60a8\u9700\u8981\u590d\u5236 DIV \u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 innerHTML \u5c5e\u6027\u3002 \/\/ \u83b7\u53d6\u8981\u590d\u5236\u7684 DIV const originalDiv = document.getElementById(&#8216;myDiv&#8217;); \/\/ \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 [&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-3493","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3493","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=3493"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3493\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}