{"id":1322,"date":"2024-11-10T10:27:30","date_gmt":"2024-11-10T02:27:30","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1322\/"},"modified":"2024-11-10T10:27:30","modified_gmt":"2024-11-10T02:27:30","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-javascript-%e4%b8%ad%e6%b7%bb%e5%8a%a0-dom-%e5%85%83%e7%b4%a0%e5%b1%9e%e6%80%a7%ef%bc%8c%e8%80%8c%e4%b8%8d%e6%8c%87%e5%ae%9a%e5%b1%9e%e6%80%a7%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1322\/","title":{"rendered":"\u5982\u4f55\u5728 JavaScript \u4e2d\u6dfb\u52a0 DOM \u5143\u7d20\u5c5e\u6027\uff0c\u800c\u4e0d\u6307\u5b9a\u5c5e\u6027\u503c\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173036667624542.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 JavaScript \u4e2d\u6dfb\u52a0 DOM \u5143\u7d20\u5c5e\u6027\uff0c\u800c\u4e0d\u6307\u5b9a\u5c5e\u6027\u503c\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 JavaScript \u4e2d\u6dfb\u52a0 DOM \u5143\u7d20\u5c5e\u6027\uff0c\u800c\u4e0d\u6307\u5b9a\u5c5e\u6027\u503c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728\u4e0d\u6307\u5b9a\u5c5e\u6027\u503c\u60c5\u51b5\u4e0b\uff0c\u5982\u4f55\u4f7f\u7528 javascript \u6dfb\u52a0 dom \u5143\u7d20<\/strong><\/p>\n<p>JavaScript \u4e2d\u7684 setAttribute \u65b9\u6cd5\u9700\u8981\u63d0\u4f9b\u5c5e\u6027\u7684\uff0c\u4f46\u6709\u65f6\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a\u5c5e\u6027\uff0c\u800c\u4e0d\u9700\u8981\u6307\u5b9a\u503c\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 createAttribute \u65b9\u6cd5\u6765\u521b\u5efa\u5c5e\u6027\uff0c\u7136\u540e\u4f7f\u7528 setAttributeNode \u65b9\u6cd5\u5c06\u8be5\u5c5e\u6027\u9644\u52a0\u5230 DOM \u5143\u7d20\u4e0a\u3002<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>const attr = document.createAttribute('primary');\nconst ele = document.createElement('div');\nele.setAttributeNode(attr);\ndocument.body.append(ele);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u5e26\u6709 primary \u5c5e\u6027\u7684 <\/p>\n<p> \u5143\u7d20\uff0c\u4f46\u8be5\u5c5e\u6027\u4e0d\u5177\u6709\u503c\u3002\u7ed3\u679c\u5982\u4e0b\uff1a <\/p>\n<pre>&lt;body&gt;\n    &lt;div primary&gt;&lt;\/div&gt;\n&lt;\/body&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 JavaScript \u4e2d\u6dfb\u52a0 DOM \u5143\u7d20\u5c5e\u6027\uff0c\u800c\u4e0d\u6307\u5b9a\u5c5e\u6027\u503c\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>\u5728\u4e0d\u6307\u5b9a\u5c5e\u6027\u503c\u60c5\u51b5\u4e0b\uff0c\u5982\u4f55\u4f7f\u7528 javascript \u6dfb\u52a0 dom \u5143\u7d20 JavaScript \u4e2d\u7684 setAttribute \u65b9\u6cd5\u9700\u8981\u63d0\u4f9b\u5c5e\u6027\u7684\uff0c\u4f46\u6709\u65f6\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a\u5c5e\u6027\uff0c\u800c\u4e0d\u9700\u8981\u6307\u5b9a\u503c\u3002 \u89e3\u51b3\u65b9\u6848\uff1a \u53ef\u4ee5\u4f7f\u7528 createAttribute \u65b9\u6cd5\u6765\u521b\u5efa\u5c5e\u6027\uff0c\u7136\u540e\u4f7f\u7528 setAttributeNode \u65b9\u6cd5\u5c06\u8be5\u5c5e\u6027\u9644\u52a0\u5230 DOM \u5143\u7d20\u4e0a\u3002 \u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b const attr = document.createAttribute(&#8216;primary&#8217;); const ele = document.createElement(&#8216;div&#8217;); ele.setAttributeNode(attr); document.body.append(ele); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u5e26\u6709 primary \u5c5e\u6027\u7684 \u5143\u7d20\uff0c\u4f46\u8be5\u5c5e\u6027\u4e0d\u5177\u6709\u503c\u3002\u7ed3\u679c\u5982\u4e0b\uff1a &lt;body&gt; &lt;div primary&gt;&lt;\/div&gt; &lt;\/body&gt; \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 JavaScript \u4e2d\u6dfb\u52a0 DOM \u5143\u7d20\u5c5e\u6027\uff0c\u800c\u4e0d\u6307\u5b9a\u5c5e\u6027\u503c\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},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1322","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1322","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=1322"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1322\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}