{"id":3198,"date":"2024-11-10T17:26:16","date_gmt":"2024-11-10T09:26:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3198\/"},"modified":"2024-11-10T17:26:16","modified_gmt":"2024-11-10T09:26:16","slug":"js%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96id","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3198\/","title":{"rendered":"js\u5982\u4f55\u83b7\u53d6id"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\u83b7\u53d6\u5143\u7d20 id \u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 document.getelementbyid() \u65b9\u6cd5\uff0c\u63a5\u53d7\u5143\u7d20 id \u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u5143\u7d20\u5f15\u7528\u3002\u4f7f\u7528 document.queryselector() \u65b9\u6cd5\uff0c\u91c7\u7528 css \u9009\u62e9\u5668\u5b57\u7b26\u4e32\uff08#id\uff09\u4f5c\u4e3a\u53c2\u6570\u3002\u4f7f\u7528 document.queryselectorall() \u65b9\u6cd5\uff0c\u91c7\u7528 css \u9009\u62e9\u5668\u5b57\u7b26\u4e32\uff08#id\uff09\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd4\u56de\u5339\u914d\u5143\u7d20\u7684 nodelist\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110806515182445.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u83b7\u53d6id\u63d2\u56fe\" alt=\"js\u5982\u4f55\u83b7\u53d6id\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u83b7\u53d6\u5143\u7d20 ID<\/strong><\/p>\n<p>JavaScript \u662f\u4e00\u79cd\u5ba2\u6237\u7aef\u811a\u672c\u8bed\u8a00\uff0c\u53ef\u7528\u4e8e\u64cd\u4f5c\u7f51\u9875\u4e2d\u7684\u5143\u7d20\u3002\u83b7\u53d6\u5143\u7d20 ID \u662f JavaScript \u4e2d\u4e00\u9879\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u53ef\u5e2e\u52a9\u60a8\u8bc6\u522b\u548c\u64cd\u7eb5\u7279\u5b9a\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u4f7f\u7528 document.getElementById()<\/strong><\/p>\n<p>\u83b7\u53d6\u5143\u7d20 ID \u7684\u6700\u76f4\u63a5\u65b9\u6cd5\u662f\u4f7f\u7528 document.getElementById() \u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u63a5\u53d7\u5143\u7d20 ID \u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5bf9\u8be5\u5143\u7d20\u7684\u5f15\u7528\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\u3002<\/p>\n<pre>const element = document.getElementById(\"my-element\");<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528 document.querySelector()<\/strong><\/p>\n<p>\u53e6\u4e00\u4e2a\u83b7\u53d6\u5143\u7d20 ID \u7684\u65b9\u6cd5\u662f\u4f7f\u7528 document.querySelector() \u65b9\u6cd5\uff0c\u5b83\u91c7\u7528 CSS \u9009\u62e9\u5668\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u3002\u8981\u6309 ID \u83b7\u53d6\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>const element = document.querySelector(\"#my-element\");<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528 document.querySelectorAll()<\/strong><\/p>\n<p>\u5982\u679c\u60a8\u9700\u8981\u83b7\u53d6\u9875\u9762\u4e2d\u5177\u6709\u76f8\u540c ID \u7684\u6240\u6709\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528 document.querySelectorAll() \u65b9\u6cd5\u3002\u5b83\u63a5\u53d7 CSS \u9009\u62e9\u5668\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5305\u542b\u5339\u914d\u5143\u7d20\u7684 NodeList\u3002<\/p>\n<pre>const elements = document.querySelectorAll(\"#my-element\");<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u83b7\u53d6\u5c5e\u6027\u503c<\/strong><\/p>\n<p>\u4e00\u65e6\u83b7\u53d6\u4e86\u5143\u7d20\u7684\u5f15\u7528\uff0c\u60a8\u5c31\u53ef\u4ee5\u8bbf\u95ee\u5176\u5c5e\u6027\u503c\uff0c\u5305\u62ec ID\u3002\u8981\u83b7\u53d6\u5143\u7d20\u7684 ID\uff0c\u53ef\u4ee5\u4f7f\u7528 id \u5c5e\u6027\uff1a<\/p>\n<pre>const elementId = element.id;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<pre>&lt;!-- HTML \u4ee3\u7801 --&gt;\n&lt;div id=\"my-element\"&gt;&lt;\/div&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    <\/p>\n<pre>\/\/ JavaScript \u4ee3\u7801\nconst element = document.getElementById(\"my-element\");\nconsole.log(element.id); \/\/ \u8f93\u51fa: \"my-element\"<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u83b7\u53d6id\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\u83b7\u53d6\u5143\u7d20 id \u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 document.getelementbyid() \u65b9\u6cd5\uff0c\u63a5\u53d7\u5143\u7d20 id \u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u5143\u7d20\u5f15\u7528\u3002\u4f7f\u7528 document.queryselector() \u65b9\u6cd5\uff0c\u91c7\u7528 css \u9009\u62e9\u5668\u5b57\u7b26\u4e32\uff08#id\uff09\u4f5c\u4e3a\u53c2\u6570\u3002\u4f7f\u7528 document.queryselectorall() \u65b9\u6cd5\uff0c\u91c7\u7528 css \u9009\u62e9\u5668\u5b57\u7b26\u4e32\uff08#id\uff09\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd4\u56de\u5339\u914d\u5143\u7d20\u7684 nodelist\u3002 \u5982\u4f55\u5728 JavaScript \u4e2d\u83b7\u53d6\u5143\u7d20 ID JavaScript \u662f\u4e00\u79cd\u5ba2\u6237\u7aef\u811a\u672c\u8bed\u8a00\uff0c\u53ef\u7528\u4e8e\u64cd\u4f5c\u7f51\u9875\u4e2d\u7684\u5143\u7d20\u3002\u83b7\u53d6\u5143\u7d20 ID \u662f JavaScript \u4e2d\u4e00\u9879\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u53ef\u5e2e\u52a9\u60a8\u8bc6\u522b\u548c\u64cd\u7eb5\u7279\u5b9a\u7684\u5143\u7d20\u3002 \u4f7f\u7528 document.getElementById() \u83b7\u53d6\u5143\u7d20 ID \u7684\u6700\u76f4\u63a5\u65b9\u6cd5\u662f\u4f7f\u7528 document.getElementById() \u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u63a5\u53d7\u5143\u7d20 ID \u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5bf9\u8be5\u5143\u7d20\u7684\u5f15\u7528\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\u3002 const element = document.getElementById(&#8220;my-element&#8221;); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 document.querySelector() \u53e6\u4e00\u4e2a\u83b7\u53d6\u5143\u7d20 ID \u7684\u65b9\u6cd5\u662f\u4f7f\u7528 document.querySelector() \u65b9\u6cd5\uff0c\u5b83\u91c7\u7528 CSS \u9009\u62e9\u5668\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u3002\u8981\u6309 ID \u83b7\u53d6\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a const [&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-3198","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3198","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=3198"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3198\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}