{"id":2501,"date":"2024-11-10T17:57:35","date_gmt":"2024-11-10T09:57:35","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2501\/"},"modified":"2024-11-10T17:57:35","modified_gmt":"2024-11-10T09:57:35","slug":"css-%e9%80%89%e6%8b%a9%e5%99%a8%e4%b8%8e%e5%8e%9f%e7%94%9f-js-%e6%93%8d%e4%bd%9c-dom%ef%bc%9a%e5%a6%82%e4%bd%95%e9%80%89%e6%8b%a9%e9%9d%9e-p-%e5%85%83%e7%b4%a0%e5%92%8c%e5%81%b6%e6%95%b0%e4%bd%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2501\/","title":{"rendered":"CSS \u9009\u62e9\u5668\u4e0e\u539f\u751f JS \u64cd\u4f5c DOM\uff1a\u5982\u4f55\u9009\u62e9\u975e p \u5143\u7d20\u548c\u5076\u6570\u4f4d\u7f6e\u7684 p \u5143\u7d20\uff1f\u5982\u4f55\u63d2\u5165\u548c\u5220\u9664 HTML\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173077869477341.jpg\" class=\"aligncenter\" title=\"CSS \u9009\u62e9\u5668\u4e0e\u539f\u751f JS \u64cd\u4f5c DOM\uff1a\u5982\u4f55\u9009\u62e9\u975e p \u5143\u7d20\u548c\u5076\u6570\u4f4d\u7f6e\u7684 p \u5143\u7d20\uff1f\u5982\u4f55\u63d2\u5165\u548c\u5220\u9664 HTML\uff1f\u63d2\u56fe\" alt=\"CSS \u9009\u62e9\u5668\u4e0e\u539f\u751f JS \u64cd\u4f5c DOM\uff1a\u5982\u4f55\u9009\u62e9\u975e p \u5143\u7d20\u548c\u5076\u6570\u4f4d\u7f6e\u7684 p \u5143\u7d20\uff1f\u5982\u4f55\u63d2\u5165\u548c\u5220\u9664 HTML\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u7b14\u8bd5\u9898\u89e3\u60d1<\/strong><\/p>\n<p>\u672c\u7bc7\u95ee\u7b54\u4e3b\u8981\u89e3\u51b3\u4e24\u9053\u7b14\u8bd5\u9898\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5398\u6e05  \u9009\u62e9\u5668\u548c\u539f\u751f js \u7684\u64cd\u7eb5 dom \u77e5\u8bc6\u3002<\/p>\n<p><strong>1. css \u9009\u62e9\u5668<\/strong><\/p>\n<p>\u95ee\u9898\uff1a\u5982\u4f55\u5728 css \u4e2d\u9009\u62e9 div \u4e2d\u7684\u975e p \u5143\u7d20\u6216\u9009\u62e9\u5076\u6570\u4f4d\u7f6e\u7684 p \u5143\u7d20\uff1f<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u7b54\u6848\uff1a<\/p>\n<ul>\n<li>\u9009\u62e9\u975e p \u5143\u7d20\uff1adiv :not(p)<\/li>\n<li>\u9009\u62e9\u5076\u6570\u4e0b\u6807 p \u5143\u7d20\uff1adiv p:nth-child(even)<\/li>\n<\/ul>\n<p><strong>2. \u539f\u751f js \u64cd\u7eb5 dom<\/strong><\/p>\n<p>\u95ee\u9898\uff1a\u5982\u4f55\u4f7f\u7528\u539f\u751f js \u5411\u5177\u6709 id=&#8221;name&#8221; \u7684 div \u4e2d\u63d2\u5165\u548c\u5220\u9664 html\uff1f<\/p>\n<p>\u7b54\u6848\uff1a<\/p>\n<ul>\n<li>\u63d2\u5165 html\uff1a<\/li>\n<\/ul>\n<pre>document.getelementbyid(\"name\").insertadjacenthtml(\"afterend\", \"&lt;span&gt;\u5f20\u4e09&lt;\/span&gt;\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\u5220\u9664 html\uff1a<\/li>\n<\/ul>\n<pre>document.getElementById(\"name\").remove()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fCSS \u9009\u62e9\u5668\u4e0e\u539f\u751f JS \u64cd\u4f5c DOM\uff1a\u5982\u4f55\u9009\u62e9\u975e p \u5143\u7d20\u548c\u5076\u6570\u4f4d\u7f6e\u7684 p \u5143\u7d20\uff1f\u5982\u4f55\u63d2\u5165\u548c\u5220\u9664 HTML\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>\u7b14\u8bd5\u9898\u89e3\u60d1 \u672c\u7bc7\u95ee\u7b54\u4e3b\u8981\u89e3\u51b3\u4e24\u9053\u7b14\u8bd5\u9898\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5398\u6e05 \u9009\u62e9\u5668\u548c\u539f\u751f js \u7684\u64cd\u7eb5 dom \u77e5\u8bc6\u3002 1. css \u9009\u62e9\u5668 \u95ee\u9898\uff1a\u5982\u4f55\u5728 css \u4e2d\u9009\u62e9 div \u4e2d\u7684\u975e p \u5143\u7d20\u6216\u9009\u62e9\u5076\u6570\u4f4d\u7f6e\u7684 p \u5143\u7d20\uff1f \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u7b54\u6848\uff1a \u9009\u62e9\u975e p \u5143\u7d20\uff1adiv :not(p) \u9009\u62e9\u5076\u6570\u4e0b\u6807 p \u5143\u7d20\uff1adiv p:nth-child(even) 2. \u539f\u751f js \u64cd\u7eb5 dom \u95ee\u9898\uff1a\u5982\u4f55\u4f7f\u7528\u539f\u751f js \u5411\u5177\u6709 id=&#8221;name&#8221; \u7684 div \u4e2d\u63d2\u5165\u548c\u5220\u9664 html\uff1f \u7b54\u6848\uff1a \u63d2\u5165 html\uff1a document.getelementbyid(&#8220;name&#8221;).insertadjacenthtml(&#8220;afterend&#8221;, &#8220;&lt;span&gt;\u5f20\u4e09&lt;\/span&gt;&#8221;); \u767b\u5f55\u540e\u590d\u5236 \u5220\u9664 html\uff1a document.getElementById(&#8220;name&#8221;).remove() \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u5c31\u662fCSS \u9009\u62e9\u5668\u4e0e\u539f\u751f JS [&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-2501","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2501","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=2501"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2501\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}