{"id":3491,"date":"2024-11-10T17:23:41","date_gmt":"2024-11-10T09:23:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3491\/"},"modified":"2024-11-10T17:23:41","modified_gmt":"2024-11-10T09:23:41","slug":"js-%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e8%8a%82%e7%82%b9","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3491\/","title":{"rendered":"js \u5982\u4f55\u83b7\u53d6\u8282\u70b9"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\uff0c\u83b7\u53d6 dom \u8282\u70b9\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1agetelementbyid\uff1a\u901a\u8fc7 id \u83b7\u53d6\u5143\u7d20\u3002getelementsbytagname\uff1a\u901a\u8fc7\u6807\u7b7e\u540d\u83b7\u53d6\u5143\u7d20\u96c6\u5408\u3002getelementsbyclassname\uff1a\u901a\u8fc7\u7c7b\u540d\u83b7\u53d6\u5143\u7d20\u96c6\u5408\u3002queryselector\uff1a\u4f7f\u7528 css \u9009\u62e9\u5668\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5339\u914d\u5143\u7d20\u3002queryselectorall\uff1a\u4f7f\u7528 css \u9009\u62e9\u5668\u83b7\u53d6\u6240\u6709\u5339\u914d\u5143\u7d20\u3002childnodes\uff1a\u83b7\u53d6\u5b50\u8282\u70b9\u96c6\u5408\uff08\u542b\u6587\u672c\u8282\u70b9\uff09\u3002children\uff1a\u83b7\u53d6\u5b50\u5143\u7d20\u96c6\u5408\uff08\u4e0d\u542b\u6587\u672c\u8282\u70b9\uff09\u3002firs\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110813370944266.jpg\" class=\"aligncenter\" title=\"js \u5982\u4f55\u83b7\u53d6\u8282\u70b9\u63d2\u56fe\" alt=\"js \u5982\u4f55\u83b7\u53d6\u8282\u70b9\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u83b7\u53d6 DOM \u8282\u70b9<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u83b7\u53d6 DOM \u8282\u70b9\u7684\u6700\u5e38\u7528\u65b9\u6cd5\u662f\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n<ol>\n<li> <strong>getElementById(id)<\/strong>\uff1a\u901a\u8fc7\u5143\u7d20\u7684 ID \u5c5e\u6027\u68c0\u7d22\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>const element = document.getElementById('my-element');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li> <strong>getElementsByTagName(tagName)<\/strong>\uff1a\u901a\u8fc7\u6807\u7b7e\u540d\u68c0\u7d22\u5143\u7d20\u7684\u96c6\u5408\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>const elements = document.getElementsByTagName('p');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li> <strong>getElementsByClassName(className)<\/strong>\uff1a\u901a\u8fc7\u7c7b\u540d\u68c0\u7d22\u5143\u7d20\u7684\u96c6\u5408\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>const elements = document.getElementsByClassName('my-class');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li> <strong>querySelector(selector)<\/strong>\uff1a\u4f7f\u7528 CSS \u9009\u62e9\u5668\u68c0\u7d22\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>const element = document.querySelector('div.my-class');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li> <strong>querySelectorAll(selector)<\/strong>\uff1a\u4f7f\u7528 CSS \u9009\u62e9\u5668\u68c0\u7d22\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>const elements = document.querySelectorAll('p.my-class');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li> <strong>childNodes<\/strong>\uff1a\u83b7\u53d6\u5143\u7d20\u7684\u5b50\u8282\u70b9\u96c6\u5408\uff0c\u5305\u62ec\u6587\u672c\u8282\u70b9\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>const childNodes = element.childNodes;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li> <strong>children<\/strong>\uff1a\u83b7\u53d6\u5143\u7d20\u7684\u5b50\u5143\u7d20\u96c6\u5408\uff0c\u4e0d\u5305\u62ec\u6587\u672c\u8282\u70b9\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>const children = element.children;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li> <strong>firstChild<\/strong>\uff1a\u83b7\u53d6\u5143\u7d20\u7684\u7b2c\u4e00\u4e2a\u5b50\u8282\u70b9\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>const firstChild = element.firstChild;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li> <strong>lastChild<\/strong>\uff1a\u83b7\u53d6\u5143\u7d20\u7684\u6700\u540e\u4e00\u4e2a\u5b50\u8282\u70b9\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>const lastChild = element.lastChild;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs \u5982\u4f55\u83b7\u53d6\u8282\u70b9\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 javascript \u4e2d\uff0c\u83b7\u53d6 dom \u8282\u70b9\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1agetelementbyid\uff1a\u901a\u8fc7 id \u83b7\u53d6\u5143\u7d20\u3002getelementsbytagname\uff1a\u901a\u8fc7\u6807\u7b7e\u540d\u83b7\u53d6\u5143\u7d20\u96c6\u5408\u3002getelementsbyclassname\uff1a\u901a\u8fc7\u7c7b\u540d\u83b7\u53d6\u5143\u7d20\u96c6\u5408\u3002queryselector\uff1a\u4f7f\u7528 css \u9009\u62e9\u5668\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5339\u914d\u5143\u7d20\u3002queryselectorall\uff1a\u4f7f\u7528 css \u9009\u62e9\u5668\u83b7\u53d6\u6240\u6709\u5339\u914d\u5143\u7d20\u3002childnodes\uff1a\u83b7\u53d6\u5b50\u8282\u70b9\u96c6\u5408\uff08\u542b\u6587\u672c\u8282\u70b9\uff09\u3002children\uff1a\u83b7\u53d6\u5b50\u5143\u7d20\u96c6\u5408\uff08\u4e0d\u542b\u6587\u672c\u8282\u70b9\uff09\u3002firs \u5982\u4f55\u83b7\u53d6 DOM \u8282\u70b9 \u5728 JavaScript \u4e2d\uff0c\u83b7\u53d6 DOM \u8282\u70b9\u7684\u6700\u5e38\u7528\u65b9\u6cd5\u662f\u4ee5\u4e0b\u51e0\u79cd\uff1a getElementById(id)\uff1a\u901a\u8fc7\u5143\u7d20\u7684 ID \u5c5e\u6027\u68c0\u7d22\u5143\u7d20\u3002\u4f8b\u5982\uff1a const element = document.getElementById(&#8216;my-element&#8217;); \u767b\u5f55\u540e\u590d\u5236 getElementsByTagName(tagName)\uff1a\u901a\u8fc7\u6807\u7b7e\u540d\u68c0\u7d22\u5143\u7d20\u7684\u96c6\u5408\u3002\u4f8b\u5982\uff1a const elements = document.getElementsByTagName(&#8216;p&#8217;); \u767b\u5f55\u540e\u590d\u5236 getElementsByClassName(className)\uff1a\u901a\u8fc7\u7c7b\u540d\u68c0\u7d22\u5143\u7d20\u7684\u96c6\u5408\u3002\u4f8b\u5982\uff1a const elements = document.getElementsByClassName(&#8216;my-class&#8217;); \u767b\u5f55\u540e\u590d\u5236 querySelector(selector)\uff1a\u4f7f\u7528 CSS \u9009\u62e9\u5668\u68c0\u7d22\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a const element = document.querySelector(&#8216;div.my-class&#8217;); \u767b\u5f55\u540e\u590d\u5236 querySelectorAll(selector)\uff1a\u4f7f\u7528 CSS \u9009\u62e9\u5668\u68c0\u7d22\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a const elements = document.querySelectorAll(&#8216;p.my-class&#8217;); \u767b\u5f55\u540e\u590d\u5236 [&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-3491","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3491","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=3491"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3491\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}