{"id":168284,"date":"2024-05-08T16:20:43","date_gmt":"2024-05-08T08:20:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/168284.html"},"modified":"2024-05-08T16:20:48","modified_gmt":"2024-05-08T08:20:48","slug":"javascript-%e4%b8%ad-query%e6%98%af%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/168284.html","title":{"rendered":"JavaScript \u4e2d query\u662f\u4ec0\u4e48\u610f\u601d"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27034420\/e166d701-bd2b-4347-9404-74ea6865e732.webp\" alt=\"JavaScript \u4e2d query\u662f\u4ec0\u4e48\u610f\u601d\" \/><\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c<strong>query\u4e3b\u8981\u6307\u5411\u7684\u662f\u67e5\u8be2\u64cd\u4f5c\uff0c\u5728Web\u5f00\u53d1\u4e2d\u7ecf\u5e38\u7528\u4e8e\u9009\u62e9\u6216\u67e5\u627eDOM\uff08\u6587\u6863\u5bf9\u8c61\u6a21\u578b\uff09\u5143\u7d20<\/strong>\u3002JavaScript\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0DOM\u67e5\u8be2\uff0c\u4f8b\u5982<code>getElementById<\/code>\u3001<code>getElementsByClassName<\/code>\u3001<code>querySelector<\/code>\u3001<code>querySelectorAll<\/code>\u7b49\u3002\u5176\u4e2d\uff0c<strong><code>querySelector<\/code>\u548c<code>querySelectorAll<\/code><\/strong> \u662f\u66f4\u73b0\u4ee3\u3001\u66f4\u5f3a\u5927\u7684\u65b9\u6cd5\uff0c\u5b83\u4eec\u5141\u8bb8\u5f00\u53d1\u8005\u4f7f\u7528CSS\u9009\u62e9\u5668\u8bed\u6cd5\u6765\u67e5\u627e\u5339\u914d\u7684\u5143\u7d20\uff0c\u6781\u5927\u63d0\u5347\u4e86\u67e5\u627e\u5143\u7d20\u7684\u7075\u6d3b\u6027\u548c\u4fbf\u6377\u6027\u3002<\/p>\n<\/p>\n<p><h3><strong>I\u3001QUERYSELECTOR \u548c QUERYSELECTORALL<\/strong><\/h3>\n<\/p>\n<p><p><strong><code>querySelector<\/code><\/strong> \u65b9\u6cd5\u8fd4\u56de\u6587\u6863\u4e2d\u4e0e\u6307\u5b9a\u9009\u62e9\u5668\u6216\u9009\u62e9\u5668\u7ec4\u5339\u914d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\u5339\u914d\u9879\uff0c\u5219\u8fd4\u56de<code>null<\/code>\u3002\u8fd9\u4e2a\u65b9\u6cd5\u662f\u9009\u62e9\u5355\u4e00\u5143\u7d20\u7684\u597d\u529e\u6cd5\uff0c\u7279\u522b\u662f\u5f53\u5143\u7d20\u5177\u6709\u552f\u4e00\u7684ID\u6216\u7c7b\u540d\u65f6\u3002\u4f8b\u5982\uff0c<code>document.querySelector(&#039;.my-class&#039;)<\/code>\u5c06\u8fd4\u56de\u6587\u6863\u4e2d\u62e5\u6709\u7c7b<code>my-class<\/code>\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong><code>querySelectorAll<\/code><\/strong> \u65b9\u6cd5\u5219\u8fd4\u56de\u6240\u6709\u7b26\u5408\u6307\u5b9a\u9009\u62e9\u5668\u7684\u5143\u7d20\u7684\u4e00\u4e2a\u9759\u6001\uff08\u4e0d\u4f1a\u968f\u7740\u6587\u6863\u53d8\u5316\uff09\u7684NodeList\u96c6\u5408\u3002\u8fd9\u5bf9\u4e8e\u9009\u62e9\u591a\u4e2a\u5143\u7d20\u5e76\u8fdb\u884c\u64cd\u4f5c\u5341\u5206\u6709\u7528\u3002\u6bd4\u5982\uff0c<code>document.querySelectorAll(&#039;div&#039;)<\/code>\u5c06\u9009\u62e9\u6587\u6863\u4e2d\u6240\u6709<code>&lt;div&gt;<\/code>\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3><strong>II\u3001DOM\u67e5\u8be2\u4e2d\u7684\u5176\u4ed6\u65b9\u6cd5<\/strong><\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u7684\u73b0\u4ee3\u67e5\u8be2\u65b9\u6cd5\uff0cJavaScript\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u4f20\u7edf\u7684DOM\u67e5\u8be2\u65b9\u6cd5\uff0c\u5b83\u4eec\u4e3b\u8981\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong><code>getElementById()<\/code><\/strong>\uff1a\u8fd9\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5b83\u6839\u636e\u7ed9\u5b9a\u7684ID\u9009\u62e9\u5355\u4e2a\u5143\u7d20\u3002\u8fd9\u4e2a\u65b9\u6cd5\u53ea\u8fd4\u56de\u4e00\u4e2a\u5143\u7d20\uff0c\u56e0\u4e3aID\u5728\u4e00\u4e2aHTML\u6587\u6863\u4e2d\u662f\u552f\u4e00\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>getElementsByClassName()<\/code><\/strong>\uff1a\u6b64\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2aHTMLCollection\u5bf9\u8c61\uff0c\u5305\u62ec\u6240\u6709\u5177\u6709\u6307\u5b9a\u7c7b\u540d\u7684\u5143\u7d20\u3002\u8fd9\u5bf9\u4e8e\u8981\u9009\u62e9\u5177\u6709\u76f8\u540c\u7c7b\u540d\u4f46\u4e0d\u540cID\u7684\u591a\u4e2a\u5143\u7d20\u5f88\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>getElementsByTagName()<\/code><\/strong>\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u6587\u6863\u4e2d\u6240\u6709\u5177\u6709\u6307\u5b9a\u6807\u7b7e\u540d\u7684\u5143\u7d20\u7684\u4e00\u4e2aHTMLCollection\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u64cd\u4f5c\u7279\u5b9a\u7c7b\u578b\u5143\u7d20\uff08\u5982\u6240\u6709\u7684<code>&lt;div&gt;<\/code>\u6216<code>&lt;a&gt;<\/code>\u6807\u7b7e\uff09\u7684\u60c5\u51b5\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3><strong>III\u3001\u4e3a\u4ec0\u4e48\u4f7f\u7528QUERY\u800c\u4e0d\u662f\u5176\u4ed6\u65b9\u6cd5<\/strong><\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u4f7f\u7528<code>querySelector<\/code>\u6216<code>querySelectorAll<\/code>\u800c\u4e0d\u662f\u5176\u4ed6DOM\u67e5\u8be2\u65b9\u6cd5\u7684\u539f\u56e0\u6709\u5f88\u591a\u3002\u9996\u5148\uff0c<strong>\u5b83\u4eec\u63d0\u4f9b\u4e86\u66f4\u5f3a\u7684\u7075\u6d3b\u6027<\/strong>\u3002\u4f7f\u7528CSS\u9009\u62e9\u5668\u4f5c\u4e3a\u53c2\u6570\uff0c\u53ef\u4ee5\u975e\u5e38\u7cbe\u786e\u5730\u6307\u5b9a\u8981\u9009\u62e9\u7684\u5143\u7d20\uff0c\u5305\u62ec\u5b83\u4eec\u7684\u7c7b\u540d\u3001ID\u3001\u5c5e\u6027\u7b49\u3002\u5176\u6b21\uff0c\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u5141\u8bb8\u76f4\u63a5\u9009\u62e9\u5b50\u5143\u7d20\u548c\u4f2a\u5143\u7d20\uff0c\u8fd9\u5728\u590d\u6742\u7684Web\u5e94\u7528\u5f00\u53d1\u4e2d\u975e\u5e38\u6709\u7528\u3002\u6700\u540e\uff0c<strong>\u4f7f\u7528\u8fd9\u4e9b\u73b0\u4ee3\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u7b80\u6d01\u3001\u66f4\u6613\u8bfb<\/strong>\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u719f\u6089CSS\u7684\u5f00\u53d1\u8005\u6765\u8bf4\u3002<\/p>\n<\/p>\n<p><h3><strong>IV\u3001\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u8003\u8651<\/strong><\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>querySelector<\/code>\u548c<code>querySelectorAll<\/code>\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u4fbf\u5229\u548c\u7075\u6d3b\u6027\uff0c\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u6b63\u786e\u7684\u67e5\u8be2\u65b9\u6cd5\u4e5f\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u505a\u51fa\u6743\u8861\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u9700\u8981\u9009\u62e9\u7684\u5143\u7d20\u5177\u6709\u552f\u4e00\u7684ID\uff0c\u90a3\u4e48\u4f7f\u7528<code>getElementById<\/code>\u53ef\u80fd\u4f1a\u662f\u66f4\u5408\u9002\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u7684\u6027\u80fd\u901a\u5e38\u8981\u6bd4<code>querySelector<\/code>\u66f4\u4f18\u3002\u540c\u6837\uff0c\u5bf9\u4e8e\u9700\u8981\u9009\u62e9\u5f88\u591a\u5177\u6709\u76f8\u540c\u7c7b\u540d\u7684\u5143\u7d20\u65f6\uff0c<code>getElementsByClassName<\/code>\u53ef\u80fd\u4f1a\u6bd4<code>querySelectorAll<\/code>\u66f4\u9ad8\u6548\uff0c\u56e0\u4e3a\u540e\u8005\u4f1a\u8fd4\u56de\u4e00\u4e2a\u9759\u6001\u7684NodeList\uff0c\u64cd\u4f5c\u8fd9\u4e2a\u5217\u8868\u53ef\u80fd\u4f1a\u6bd4\u64cd\u4f5cHTMLCollection\u66f4\u4e3a\u590d\u6742\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u4e86\u89e3\u5e76\u638c\u63e1JavaScript\u4e2d\u5404\u79cd\u67e5\u8be2\u65b9\u6cd5\u7684\u7279\u70b9\u4e0e\u7528\u6cd5\uff0c\u5bf9\u4e8e\u9ad8\u6548\u5730\u64cd\u4f5cDOM\uff0c\u6784\u5efa\u54cd\u5e94\u8fc5\u901f\u548c\u7528\u6237\u53cb\u597d\u7684Web\u5e94\u7528\u81f3\u5173\u91cd\u8981\u3002<em>dom<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n query<\/em> \u529f\u80fd\u4e0d\u518d\u5c40\u9650\u4e8e\u7b80\u5355\u7684\u6587\u6863\u67e5\u627e\uff0c\u800c\u662f\u6210\u4e3a\u4e86Web\u5f00\u53d1\u4e2d\u5fc5\u4e0d\u53ef\u5c11\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u8ba9\u6211\u4eec\u80fd\u591f\u6784\u5efa\u66f4\u590d\u6742\u3001\u66f4\u52a8\u6001\u548c\u66f4\u4e92\u52a8\u7684\u7528\u6237\u754c\u9762\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. query\u5728JavaScript\u4e2d\u662f\u4ec0\u4e48\u542b\u4e49\uff1f<\/strong><\/p>\n<p>\u5728JavaScript\u4e2d\uff0cquery\u662f\u4e00\u4e2a\u7528\u4e8e\u64cd\u4f5c\u6587\u6863\u4e2d\u5143\u7d20\u7684\u65b9\u6cd5\u6216\u5c5e\u6027\u3002\u5b83\u901a\u5e38\u6307\u7684\u662f\u901a\u8fc7\u4f7f\u7528\u9009\u62e9\u5668\u6765\u83b7\u53d6\u6307\u5b9a\u5143\u7d20\u7684\u4e00\u79cd\u884c\u4e3a\u3002<\/p>\n<p><strong>2. JavaScript\u4e2d\u7684query\u7528\u4e8e\u54ea\u4e9b\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p>JavaScript\u4e2d\u7684query\u53ef\u4ee5\u7528\u4e8e\u591a\u79cd\u64cd\u4f5c\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u662f\u901a\u8fc7\u4f7f\u7528\u9009\u62e9\u5668\u6765\u83b7\u53d6\u6587\u6863\u4e2d\u7684\u5143\u7d20\u3002\u901a\u8fc7query\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5143\u7d20\u7684id\u3001class\u3001\u6807\u7b7e\u540d\u7b49\u5c5e\u6027\u6765\u51c6\u786e\u5730\u9009\u62e9\u548c\u64cd\u4f5c\u5143\u7d20\u3002<\/p>\n<p>\u9664\u6b64\u4e4b\u5916\uff0cquery\u8fd8\u53ef\u4ee5\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\uff0c\u4f8b\u5982\u4fee\u6539\u5143\u7d20\u7684\u6837\u5f0f\u3001\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u3001\u4fee\u6539\u5143\u7d20\u7684\u5c5e\u6027\u503c\u7b49\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5728JavaScript\u4e2d\u4f7f\u7528query\u6765\u9009\u62e9\u5143\u7d20\uff1f<\/strong><\/p>\n<p>\u8981\u4f7f\u7528query\u6765\u9009\u62e9\u5143\u7d20\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5185\u7f6e\u7684JavaScript\u65b9\u6cd5\uff0c\u5982<code>document.querySelector()<\/code>\u548c<code>document.querySelectorAll()<\/code>\u3002<\/p>\n<p><code>document.querySelector()<\/code>\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2aCSS\u9009\u62e9\u5668\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u6587\u6863\u4e2d\u5339\u914d\u8be5\u9009\u62e9\u5668\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u5982\u679c\u6ca1\u6709\u5339\u914d\u7684\u5143\u7d20\uff0c\u5219\u8fd4\u56denull\u3002<\/p>\n<p><code>document.querySelectorAll()<\/code>\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2aCSS\u9009\u62e9\u5668\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u6587\u6863\u4e2d\u5339\u914d\u8be5\u9009\u62e9\u5668\u7684\u6240\u6709\u5143\u7d20\uff0c\u4ee5NodeList\u7684\u5f62\u5f0f\u8fd4\u56de\u3002\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u591a\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<p>\u4f7f\u7528\u8fd9\u4e9bquery\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728JavaScript\u4e2d\u9009\u62e9\u548c\u64cd\u4f5c\u9700\u8981\u7684\u5143\u7d20\uff0c\u5b9e\u73b0\u52a8\u6001\u7684\u7f51\u9875\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u4e2d\uff0cquery\u4e3b\u8981\u6307\u5411\u7684\u662f\u67e5\u8be2\u64cd\u4f5c\uff0c\u5728Web\u5f00\u53d1\u4e2d\u7ecf\u5e38\u7528\u4e8e\u9009\u62e9\u6216\u67e5\u627eDOM\uff08\u6587\u6863\u5bf9\u8c61\u6a21\u578b [&hellip;]","protected":false},"author":3,"featured_media":168292,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/168284"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=168284"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/168284\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/168292"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=168284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=168284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=168284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}