{"id":5036,"date":"2015-11-05T23:31:12","date_gmt":"2015-11-05T15:31:12","guid":{"rendered":"http:\/\/www.zhangxinxu.com\/wordpress\/?p=5036"},"modified":"2019-02-09T11:57:11","modified_gmt":"2019-02-09T03:57:11","slug":"know-dom-queryselectorall","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2015\/11\/know-dom-queryselectorall\/","title":{"rendered":"DOM\u5143\u7d20querySelectorAll\u53ef\u80fd\u8ba9\u4f60\u610f\u5916\u7684\u7279\u6027\u8868\u73b0"},"content":{"rendered":"<p>by <a href=\"http:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"http:\/\/www.zhangxinxu.com\/\">http:\/\/www.zhangxinxu.com<\/a><br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=5036\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=5036<\/a><\/p>\n<h3>\u4e00\u3001\u65f6\u95f4\u7d27\u6025\uff0c\u5e9f\u8bdd\u5c11\u8bf4<\/h3>\n<p>\u672c\u6587\u6240\u5728\u7684\u9875\u9762\u85cf\u533f\u4e86\u4e0b\u9762\u8fd9\u4e9b\u4ee3\u7801\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>&lt;img id=\"outside\">\r\n&lt;div id=\"my-id\">\r\n    &lt;img id=\"inside\">\r\n    &lt;div class=\"lonely\">&lt;\/div>\r\n    &lt;div class=\"outer\">\r\n        &lt;div class=\"inner\">&lt;\/div>\r\n    &lt;\/div>\r\n&lt;\/div><\/pre>\n<\/div>\n<p>\u5c31\u662f\u4e0b\u9762\u8fd9\u6837\u7684\u8868\u73b0\uff08\u4e3a\u4e86\u4fbf\u4e8e\u89c2\u5bdf\uff0c\u6211\u52a0\u4e86\u8fb9\u6846\u80cc\u666f\u8272\u548c\u6587\u5b57\uff09\uff1a<\/p>\n<style>.test-highlight{background-color: #f0f3f9;max-width:600px;padding:5px;}.test-highlight div{padding:5px;}#my-id{background-color:#fff;}.lonely{border:5px solid #cd0000;}.outer{border:5px solid #34538b;}.inner{border:5px solid #333;}<\/style>\n<div class=\"test-highlight\"><img decoding=\"async\" id=\"outside\" src=\"http:\/\/image.zhangxinxu.com\/image\/study\/s\/s128\/mm1.jpg\"><\/p>\n<div id=\"my-id\">\n        <img decoding=\"async\" id=\"inside\" src=\"http:\/\/image.zhangxinxu.com\/image\/study\/s\/s128\/mm1.jpg\"><\/p>\n<div class=\"lonely\">\u5b64\u5bb6\u5be1\u4eba<\/div>\n<div class=\"outer\">outer-inner\u4e4b\u95f4<\/p>\n<div class=\"inner\">\u6700\u5185\u90e8<\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<p>\u9996\u5148\u8bf4\u70b9\u5927\u5bb6\u90fd\u77e5\u9053\u7684\u70ed\u70ed\u8eab\u3002<\/p>\n<ul>\n<li><code>querySelector<\/code>\u548c<code>querySelectorAll<\/code> IE8+\u6d4f\u89c8\u5668\u652f\u6301\u3002<\/li>\n<li><code>querySelector<\/code>\u8fd4\u56de\u7684\u662f\u5355\u4e2aDOM\u5143\u7d20\uff1b<code>querySelectorAll<\/code>\u8fd4\u56de\u7684\u662f<code>NodeList<\/code>.<\/li>\n<li>\u6211\u4eec\u4e00\u822c\u7528\u7684\u591a\u7684\u662f<code>document.querySelectorAll<\/code>, \u5b9e\u9645\u4e0a\uff0c\u4e5f\u652f\u6301<code>dom.querySelectorAll<\/code>.\u4f8b\u5982\uff1a\n<div class=\"zxx_code\">\n<pre>document.querySelector(\"#my-id\").querySelectorAll(\"img\")<\/pre>\n<\/div>\n<p>\u9009\u62e9\u7684\u5c31\u662f\u91cc\u9762\u8fd9\u4e2a\u59b9\u5b50\u3002\u4f8b\u5982\uff0c\u6211\u5728\u63a7\u5236\u53f0\u8f93\u51fa\u8be5\u9009\u62e9<code>NodeList<\/code>\u7684\u957f\u5ea6\u548c<code>id<\/code>\uff0c\u5982\u4e0b\u622a\u56fe\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201511\/2015-11-05_224459.png\" width=\"486\" height=\"164\" alt=\"\u63a7\u5236\u53f0\u8f93\u51fa\u7684\u9009\u62e9\u7684\u56fe\u7247\u7ed3\u679c\" class=\"alignnone\" \/>\n<\/li>\n<\/ul>\n<p>\u597d\u4e86\uff0c\u4e0a\u9762\u90fd\u662f\u4f17\u6240\u5468\u77e5\u7684\uff0c\u597d\uff0c\u4e0b\u9762\u5f00\u59cb\u5c55\u793a\u70b9\u6709\u610f\u601d\u7684\u3002<\/p>\n<p>\u5927\u5bb6\u770b\u4e0b\u4e0b\u97622\u884c\u7b80\u5355\u7684\u67e5\u8be2\u8bed\u53e5\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>document.querySelectorAll(\"#my-id div div\");<\/pre>\n<\/div>\n<div class=\"zxx_code\">\n<pre>document.querySelector(\"#my-id\").querySelectorAll(\"div div\");<\/pre>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201511\/yixiu.jpg\" width=\"96\" height=\"88\" alt=\"\u4e00\u4f11\u54e5\" class=\"alignleft mr20\" \/> \u63d0\u95ee\uff1a\u4e0a\u9762\u4e24\u4e2a\u8bed\u53e5\u8fd4\u56de\u7684<code>NodeList<\/code>\u7684\u5185\u5bb9\u662f\u5426\u662f\u4e00\u6837\u7684\uff1f<\/p>\n<p>\u7ed9\u5927\u5bb61\u5206\u949f\u7684\u65f6\u95f4\u601d\u8003\u4e0b\u3002<\/p>\n<p><span class=\"s\">\/\/zxx: \u5047\u8bbe1\u5206\u949f\u5df2\u7ecf\u8fc7\u53bb\u4e86<\/span><\/p>\n<p>\u597d\u4e86\uff0c\u7b54\u6848\u662f\uff1a<strong>\u4e0d\u4e00\u6837\u7684<\/strong>\u3002\u4f30\u8ba1\u4e0d\u5c11\u4eba\u8ddf\u6211\u4e00\u6837\uff0c\u4f1a\u8ba4\u4e3a\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>document.querySelectorAll(\"#my-id div div\").length === 1;<\/pre>\n<\/div>\n<div class=\"zxx_code\">\n<pre>document.querySelector(\"#my-id\").querySelectorAll(\"div div\").length === 3;<\/pre>\n<\/div>\n<p>\u5927\u5bb6\u5982\u679c\u6709\u7591\u95ee\uff0c\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u6d4b\u8bd5\u4e0b\uff0c\u4e0b\u56fe\u5c31\u662f\u6211\u81ea\u5df1\u6d4b\u8bd5\u7684\u7ed3\u679c\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201511\/2015-11-05_225735.png\" width=\"509\" height=\"175\" alt=\"\u957f\u5ea6\u7ed3\u679c\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p>\u4e3a\u5565\u4f1a\u8fd9\u6837\uff1f<\/p>\n<p>\u7b2c\u4e00\u4e2a\u7b26\u5408\u6211\u4eec\u7684\u7406\u89e3\uff0c\u4e0d\u89e3\u91ca\u3002\u90a3\u4e0b\u4e00\u4e2a\u8bed\u53e5\uff0c\u4e3a\u4f55\u8fd4\u56de\u7684<code>NodeList<\/code>\u957f\u5ea6\u662f<code>3<\/code>\u5462\uff1f<\/p>\n<p>\u9996\u5148\uff0c\u904d\u5386\u8be5<code>NodeList<\/code>\u4f1a\u53d1\u73b0\uff0c\u67e5\u8be2\u7684\u4e09\u4e2adom\u5143\u7d20\u4e3a\uff1a<code>div.lonely<\/code>, <code>div.outer<\/code>, <code>div.inner<\/code>.<\/p>\n<p>\u5947\u602a\uff0c\u5947\u602a\uff0c\u600e\u4e48\u4f1a\u662f3\u4e2a\u5462\uff1f<\/p>\n<p>jQuery\u4e2d\u6709\u4e2a<code>find()<\/code>\u65b9\u6cd5\uff0c\u5927\u5bb6\u5f88\u53ef\u80fd\u53d7\u5230\u8fd9\u4e2a\u65b9\u6cd5\u5f71\u54cd\uff0c\u5bfc\u81f4\u51fa\u73b0\u4e86\u4e00\u4e9b\u8ba4\u77e5\u7684\u95ee\u9898\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>$(\"#my-id\").find(\"div div\").length === 1;<\/pre>\n<\/div>\n<p>\u5982\u679c\u4f7f\u7528<code>find<\/code>\u65b9\u6cd5\uff0c\u5219\u662f<code>1<\/code>\u4e2a\u5339\u914d\uff1b\u7531\u4e8e\u7ed3\u6784\u548c\u4f5c\u7528\u7c7b\u4f3c\uff0c\u6211\u4eec\u5f88\u81ea\u7136\u7591\u95ee\u539f\u751f\u7684<code>querySelectorAll<\/code>\u4e5f\u662f\u8fd9\u4e2a\u5957\u8def\u3002\u771f\u662f\u592a\u9519\u7279\u9519\uff01\uff01<\/p>\n<p>\u8981\u89e3\u91ca\uff0c\u4e3a\u4f55<code>NodeList<\/code>\u957f\u5ea6\u662f<code>3<\/code>\uff0c\u53ea\u8981\u4e00\u53e5\u8bdd\u5c31\u53ef\u4ee5\u4e86\uff0c\u6211\u7279\u610f\u52a0\u7c97\u6807\u7ea2\uff1a<\/p>\n<p><strong style=\"color:#cd0000;\">CSS\u9009\u62e9\u5668\u662f\u72ec\u7acb\u4e8e\u6574\u4e2a\u9875\u9762\u7684\uff01<\/strong><\/p>\n<p>\u4ec0\u4e48\u610f\u601d\u5462\uff1f\u6bd4\u5982\u8bf4\u4f60\u5728\u9875\u9762\u5f88\u6df1\u7684\u4e00\u4e2aDOM\u91cc\u9762\u5199\u4e0a\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>&lt;style>\r\ndiv div { }\r\n&lt;\/style><\/pre>\n<\/div>\n<p>\u6574\u4e2a\u7f51\u9875\uff0c\u5305\u62ec\u7236\u7ea7\uff0c\u53ea\u8981\u662f\u6ee1\u8db3<code>div div<\/code>\u7236\u5b50\u5173\u7cfb\u7684\u5143\u7d20\uff0c\u5168\u90e8\u4f1a\u88ab\u9009\u4e2d\uff0c\u5bf9\u5427\uff0c\u8fd9\u4e2a\u5927\u5bb6\u5e94\u8be5\u90fd\u6e05\u695a\u7684\u3002<\/p>\n<p>\u8fd9\u91cc\u7684<code>querySelectorAll<\/code>\u91cc\u9762\u7684\u9009\u62e9\u5668\u4e5f\u540c\u6837\u662f\u8fd9\u4e5f\u5168\u5c40\u7279\u6027\u3002<code>document.querySelector(\"#my-id\").querySelectorAll(\"div div\")<\/code>\u7ffb\u8bd1\u6210\u767d\u8bdd\u6587\u5c31\u662f\uff1a\u67e5\u8be2<code>#my-id<\/code>\u7684\u5b50\u5143\u7d20\uff0c\u540c\u65f6\u6ee1\u8db3<strong>\u6574\u4e2a\u9875\u9762\u4e0b<\/strong><code>div div<\/code>\u9009\u62e9\u5668\u6761\u4ef6\u7684DOM\u5143\u7d20\u4eec\u3002<\/p>\n<p>\u6211\u4eec\u9875\u9762\u5f80\u4e0a\u6eda\u52a8\u770b\u770b\u539f\u59cb\u7684HTML\u7ed3\u6784\uff0c\u4f1a\u53d1\u73b0\uff0c\u5728\u5168\u5c40\u89c6\u91ce\u4e0b\uff0c<code>div.lonely<\/code>, <code>div.outer<\/code>, <code>div.inner<\/code>\u5168\u90e8\u90fd\u6ee1\u8db3<code>div div<\/code>\u8fd9\u4e2a\u9009\u62e9\u5668\u6761\u4ef6\uff0c\u4e8e\u662f\uff0c\u6700\u7ec8\u8fd4\u56de\u7684\u957f\u5ea6\u4e3a<code>3<\/code>.<\/p>\n<h3>\u4e8c\u3001:scope\u4e0e\u533a\u57df\u9009\u62e9\u9650\u5236<\/h3>\n<p>\u5176\u5b9e\uff0c\u8981\u60f3<code>querySelectorAll<\/code>\u540e\u9762\u9009\u62e9\u5668\u4e0d\u53d7\u5168\u5c40\u5f71\u54cd\uff0c\u4e5f\u662f\u6709\u529e\u6cd5\u7684\uff0c\u5c31\u662f\u4f7f\u7528\u76ee\u524d\u8fd8<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/%3Ascope\" target=\"_blank\">\u5904\u4e8e\u5b9e\u9a8c\u9636\u6bb5<\/a>\u7684<code>:scope<\/code>\u4f2a\u7c7b\uff0c\u5176\u4f5c\u7528\u5c31\u662f\u8ba9CSS\u662f\u5728\u67d0\u4e00\u8303\u56f4\u5185\u4f7f\u7528\u3002\u6b64\u4f2a\u7c7b\u5728CSS\u4e2d\u4f7f\u7528\u662f\u5927\u5934\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u5728<code>querySelectorAll<\/code>\u8bed\u53e5\u4e2d\u4f7f\u7528\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>document.querySelector(\"#my-id\").querySelectorAll(\":scope div div\");<\/pre>\n<\/div>\n<p><a href=\"http:\/\/caniuse.com\/style-scoped\/embed\" rel=\"nofollow\" target=\"_blank\">\u517c\u5bb9\u6027<\/a>\u5982\u4e0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201801\/2018-01-09_003351.png\" width=\"565\" height=\"267\" alt=\":scope\u517c\u5bb9\u6027\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p><del datetime=\"2018-01-08T16:34:07+00:00\">\u6211\u5199\u6b64\u6587\u65f6\u5019\u662f15\u5e7411\u6708\u521d\uff0c\u76ee\u524d\u57fa\u672c\u4e0a\u5c31FireFox\u6d4f\u89c8\u5668\u652f\u6301\uff0c\u6211\u4f30\u8ba1\uff0c\u4ee5\u540e\uff0c\u4f1a\u652f\u6301\u8d8a\u6765\u8d8a\u591a\u7684\u3002\u4e3a\u4ec0\u4e48\u5462\uff1f<\/del><\/p>\n<p><del datetime=\"2018-01-08T16:34:07+00:00\">\u56e0\u4e3aWeb Components\u9700\u8981\u5b83\uff0c\u53ef\u4ee5\u5b9e\u73b0\u771f\u6b63\u72ec\u7acb\u5c01\u88c5\uff0c\u4e0d\u4f1a\u53d7\u5916\u754c\u5f71\u54cd\u7684HTML\u7ec4\u4ef6\u3002<\/del><\/p>\n<p><del datetime=\"2018-01-08T16:34:07+00:00\">\u5173\u4e8e<code>:scope<\/code>\u76ee\u524d\u652f\u6301\u5c1a\u6d45\uff0c\u65f6\u673a\u672a\u5230\uff0c\u6211\u5c31\u6ca1\u5fc5\u8981\u4e71\u5c55\u5f00\u4e86\uff0c\u70b9\u5230\u4e3a\u6b62\u3002<\/del><\/p>\n<p><strong>\u66f4\u65b0\u4e8e2018-01-09<\/strong><br \/>\n\u73b0\u5728\u5728\u770b\u62112, 3\u5e74\u524d\u8bf4\u7684\u4e0a\u9762\u8fd9\u6bb5\u8bdd\uff0c\u53d1\u73b0\u8138\u6253\u5f97\u556a\u556a\u54cd\u3002<code>:scope<\/code>\u4f2a\u7c7b\u770b\u6765\u662f\u652f\u6301\u8d8a\u6765\u8d8a\u5c11\u4e86\u3002<\/p>\n<p><strong>\u66f4\u65b0\u4e8e2019-02-09<\/strong><br \/>\n\u53c8\u53cd\u8f6c\u4e86\uff0c<code>:scope<\/code>\u4f2a\u7c7b\u5728CSS\u4e2d\u867d\u7136\u4e0d\u652f\u6301\uff0c\u4f46\u662f\u5728querySelectorAll\u4e2d\u662f\u6709\u6548\u7684\u3002<\/p>\n<h3>\u4e09\u3001\u7ed3\u8bed\u8fd8\u662f\u8981\u7684<\/h3>\n<p>\u53c2\u8003\u6587\u7ae0\uff1a<a href=\"https:\/\/www.lvh.io\/posts\/queryselectorall-from-an-element-probably-doesnt-do-what-you-think-it-does.html#\">querySelectorAll from an element probably doesn&#8217;t do what you think it does<\/a><\/p>\n<p>\u611f\u8c22\u9605\u8bfb\uff0c\u6b22\u8fce\u7ea0\u9519\uff0c\u6b22\u8fce\u4ea4\u6d41\uff01<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/mat1.gtimg.com\/www\/mb\/images\/face\/14.gif\" align=\"absmiddle\"><\/p>\n<p><span class=\"s\">\/\/\u4f17\u4eba: \u4f60\u4e2b\u9017\u6211\u5462\uff0c\u8fd9\u4e5f\u80fd\u53eb\u7ed3\u8bed\u2026\u2026<\/span><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u5305\u542b\u811a\u672c\u884c\u4e3a\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=5036\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=5036<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u5185\u5bb9\u6b63\u5982\u6807\u9898\u6240\u793a\uff0c\u5c31\u662f\u4ecb\u7ecd\u5927\u5bb6\u53ef\u80fd\u7684\u5bf9DOM\u5143\u7d20querySelectorAll\u65b9\u6cd5\u4e00\u4e9b\u8ba4\u77e5\u4e0d\u6e05\u695a\u7684\u5730\u65b9\u3002\u6ca1\u6709Demo\u9875\u9762\uff0c\u4f46\u662f\u6709\u6e90\u4ee3\u7801\uff0c\u6709\u622a\u56fe\u3002\u672c\u6587\u5185\u5bb9\u8fd8\u884c\uff0c\u6d45\u663e\u6613\u61c2\uff0c\u4e5f\u5bb9\u6613\u6d88\u5316\u548c\u5438\u6536\uff0c\u7b97\u662f\u4e0d\u9519\u7684tips\u77e5\u8bc6get\u70b9\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180],"tags":[1024,182,1023,568,828,212],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/5036"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=5036"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/5036\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=5036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=5036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=5036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}