{"id":2898,"date":"2024-11-10T16:50:02","date_gmt":"2024-11-10T08:50:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2898\/"},"modified":"2024-11-10T16:50:02","modified_gmt":"2024-11-10T08:50:02","slug":"%e4%bd%bf%e7%94%a8-outerhtml-%e6%9b%bf%e6%8d%a2-html-%e7%89%87%e6%ae%b5%e5%90%8e%ef%bc%8c%e6%b7%bb%e5%8a%a0%e7%9a%84%e5%85%83%e7%b4%a0%e6%97%a0%e6%b3%95%e8%a7%a6%e5%8f%91%e7%82%b9%e5%87%bb%e4%ba%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2898\/","title":{"rendered":"\u4f7f\u7528 outerHTML \u66ff\u6362 HTML \u7247\u6bb5\u540e\uff0c\u6dfb\u52a0\u7684\u5143\u7d20\u65e0\u6cd5\u89e6\u53d1\u70b9\u51fb\u4e8b\u4ef6\u600e\u4e48\u529e\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173090451095260.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 outerHTML \u66ff\u6362 HTML \u7247\u6bb5\u540e\uff0c\u6dfb\u52a0\u7684\u5143\u7d20\u65e0\u6cd5\u89e6\u53d1\u70b9\u51fb\u4e8b\u4ef6\u600e\u4e48\u529e\uff1f\u63d2\u56fe\" alt=\"\u4f7f\u7528 outerHTML \u66ff\u6362 HTML \u7247\u6bb5\u540e\uff0c\u6dfb\u52a0\u7684\u5143\u7d20\u65e0\u6cd5\u89e6\u53d1\u70b9\u51fb\u4e8b\u4ef6\u600e\u4e48\u529e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4f7f\u7528 outerhtml \u6dfb\u52a0\u7684\u5143\u7d20\u65e0\u6cd5\u89e6\u53d1\u7684\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 outerHTML \u66ff\u6362 HTML \u7247\u6bb5\u65f6\uff0c\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\uff0c\u5373\u6dfb\u52a0\u7684\u5143\u7d20\u65e0\u6cd5\u89e6\u53d1\u4e8b\u4ef6\u76d1\u542c\u5668\u3002\u672c\u6587\u5c06\u5206\u6790\u8fd9\u4e00\u95ee\u9898\u5e76\u63d0\u4f9b\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p><strong>\u95ee\u9898\u539f\u56e0<\/strong><\/p>\n<p>\u5f53\u4f7f\u7528 outerHTML \u66ff\u6362 HTML \u7247\u6bb5\u65f6\uff0c\u65b0\u5143\u7d20\u5c06\u88ab\u91cd\u65b0\u521b\u5efa\u3002\u867d\u7136\u5176\u5185\u5bb9\u548c\u6837\u5f0f\u5f97\u4ee5\u4fdd\u7559\uff0c\u4f46\u4e8b\u4ef6\u76d1\u542c\u5668\u5c06\u4e22\u5931\u3002\u8fd9\u662f\u56e0\u4e3a\u4e8b\u4ef6\u76d1\u542c\u5668\u662f\u9644\u52a0\u5230 DOM \u5143\u7d20\u4e0a\u7684\uff0c\u800c\u975e\u5176 HTML \u4ee3\u7801\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<p><strong>\u76f4\u63a5\u76d1\u542c\u5bb9\u5668\u5143\u7d20<\/strong><\/p>\n<p>\u76f4\u63a5\u76d1\u542c\u5bb9\u5668 div \u5143\u7d20\u7684\u70b9\u51fb\u4e8b\u4ef6\uff0c\u7136\u540e\u68c0\u67e5\u4e8b\u4ef6\u7684\u76ee\u6807\u662f\u5426\u662f\u5e0c\u671b\u89e6\u53d1\u4e8b\u4ef6\u76d1\u542c\u5668\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5728\u7ed9\u5b9a\u7684\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u5c06\u70b9\u51fb\u76d1\u542c\u5668\u9644\u52a0\u5230 templateInputRef \u5143\u7d20\uff1a<\/p>\n<pre>handleClick(e) {\n    if (e.target.nodeName === 'SPAN') {\n        \/\/ \u5982\u679c\u662f\u5220\u9664\u6309\u94ae\uff0c\u5220\u9664\u6807\u7b7e\n        e.target.parentNode.remove();\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u901a\u8fc7\u5408\u6210\u4e8b\u4ef6\u89e6\u53d1<\/strong><\/p>\n<p>\u5982\u679c\u5fc5\u987b\u5728\u5b50\u5143\u7d20\u4e0a\u6dfb\u52a0\u4e8b\u4ef6\u76d1\u542c\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528\u5408\u6210\u4e8b\u4ef6\u89e6\u53d1\u3002\u8fd9\u6d89\u53ca\u624b\u52a8\u89e6\u53d1\u5b50\u5143\u7d20\u7684\u70b9\u51fb\u4e8b\u4ef6\uff0c\u800c\u4e0d\u662f\u4f9d\u9760\u7528\u6237\u7684\u5b9e\u9645\u70b9\u51fb\uff1a<\/p>\n<pre>deleteBtn.addEventListener('click', () =&gt; {\n    node.dispatchEvent(new Event('click', { bubbles: true }));\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u66ff\u4ee3\u65b9\u6cd5<\/strong><\/p>\n<p>\u9664\u4e86\u4e0a\u8ff0\u89e3\u51b3\u65b9\u6848\u5916\uff0c\u8fd8\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u66ff\u4ee3\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u4f7f\u7528\u539f\u751f DOM API\uff1a<\/strong>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u76f4\u63a5\u4fee\u6539 DOM \u5143\u7d20\uff1a<\/p>\n<pre>templateInputRef.innerHTML = ...;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u4f7f\u7528 Vue.js \u6e32\u67d3\uff1a<\/strong>\u4f7f\u7528 Vue.js \u6e32\u67d3 HTML \u4ee3\u7801\uff1a<\/p>\n<pre>this.templateInputRef.innerHTML = ...;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li> <strong>\u4f7f\u7528\u5e93\uff1a<\/strong>\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982 snabbdom \u6216 lit-html\uff0c\u53ef\u4ee5\u7b80\u5316\u66ff\u6362 DOM \u7247\u6bb5\u7684\u8fc7\u7a0b\uff0c\u540c\u65f6\u4fdd\u7559\u4e8b\u4ef6\u76d1\u542c\u5668\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 outerHTML \u66ff\u6362 HTML \u7247\u6bb5\u540e\uff0c\u6dfb\u52a0\u7684\u5143\u7d20\u65e0\u6cd5\u89e6\u53d1\u600e\u4e48\u529e\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>\u4f7f\u7528 outerhtml \u6dfb\u52a0\u7684\u5143\u7d20\u65e0\u6cd5\u89e6\u53d1\u7684\u89e3\u51b3\u65b9\u6cd5 \u5728\u4f7f\u7528 outerHTML \u66ff\u6362 HTML \u7247\u6bb5\u65f6\uff0c\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\uff0c\u5373\u6dfb\u52a0\u7684\u5143\u7d20\u65e0\u6cd5\u89e6\u53d1\u4e8b\u4ef6\u76d1\u542c\u5668\u3002\u672c\u6587\u5c06\u5206\u6790\u8fd9\u4e00\u95ee\u9898\u5e76\u63d0\u4f9b\u89e3\u51b3\u65b9\u6848\u3002 \u95ee\u9898\u539f\u56e0 \u5f53\u4f7f\u7528 outerHTML \u66ff\u6362 HTML \u7247\u6bb5\u65f6\uff0c\u65b0\u5143\u7d20\u5c06\u88ab\u91cd\u65b0\u521b\u5efa\u3002\u867d\u7136\u5176\u5185\u5bb9\u548c\u6837\u5f0f\u5f97\u4ee5\u4fdd\u7559\uff0c\u4f46\u4e8b\u4ef6\u76d1\u542c\u5668\u5c06\u4e22\u5931\u3002\u8fd9\u662f\u56e0\u4e3a\u4e8b\u4ef6\u76d1\u542c\u5668\u662f\u9644\u52a0\u5230 DOM \u5143\u7d20\u4e0a\u7684\uff0c\u800c\u975e\u5176 HTML \u4ee3\u7801\u3002 \u89e3\u51b3\u65b9\u6848 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u89e3\u51b3\u65b9\u6848\uff1a \u76f4\u63a5\u76d1\u542c\u5bb9\u5668\u5143\u7d20 \u76f4\u63a5\u76d1\u542c\u5bb9\u5668 div \u5143\u7d20\u7684\u70b9\u51fb\u4e8b\u4ef6\uff0c\u7136\u540e\u68c0\u67e5\u4e8b\u4ef6\u7684\u76ee\u6807\u662f\u5426\u662f\u5e0c\u671b\u89e6\u53d1\u4e8b\u4ef6\u76d1\u542c\u5668\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5728\u7ed9\u5b9a\u7684\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u5c06\u70b9\u51fb\u76d1\u542c\u5668\u9644\u52a0\u5230 templateInputRef \u5143\u7d20\uff1a handleClick(e) { if (e.target.nodeName === &#8216;SPAN&#8217;) { \/\/ \u5982\u679c\u662f\u5220\u9664\u6309\u94ae\uff0c\u5220\u9664\u6807\u7b7e e.target.parentNode.remove(); } } \u767b\u5f55\u540e\u590d\u5236 \u901a\u8fc7\u5408\u6210\u4e8b\u4ef6\u89e6\u53d1 \u5982\u679c\u5fc5\u987b\u5728\u5b50\u5143\u7d20\u4e0a\u6dfb\u52a0\u4e8b\u4ef6\u76d1\u542c\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528\u5408\u6210\u4e8b\u4ef6\u89e6\u53d1\u3002\u8fd9\u6d89\u53ca\u624b\u52a8\u89e6\u53d1\u5b50\u5143\u7d20\u7684\u70b9\u51fb\u4e8b\u4ef6\uff0c\u800c\u4e0d\u662f\u4f9d\u9760\u7528\u6237\u7684\u5b9e\u9645\u70b9\u51fb\uff1a deleteBtn.addEventListener(&#8216;click&#8217;, () =&gt; { node.dispatchEvent(new Event(&#8216;click&#8217;, { bubbles: true })); }); \u767b\u5f55\u540e\u590d\u5236 \u66ff\u4ee3\u65b9\u6cd5 [&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-2898","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2898","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=2898"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2898\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}