{"id":1789,"date":"2017-08-16T16:01:23","date_gmt":"2017-08-16T08:01:23","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1789"},"modified":"2017-08-16T16:01:23","modified_gmt":"2017-08-16T08:01:23","slug":"html5%e7%9a%84document-queryselectorall%e4%bd%bf%e7%94%a8%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1789","title":{"rendered":"HTML5\u7684document.querySelectorAll\u4f7f\u7528\u6559\u7a0b"},"content":{"rendered":"<p>\n\t\u66fe\u51e0\u4f55\u65f6\uff0cjQuery\u4e00\u5ea6\u662f\u6211\u4eec\u4f7f\u7528\u7387\u6700\u9ad8\u7684\u7b2c\u4e09\u65b9\u6846\u67b6\u4e86\u3002\u4eba\u4eec\u548c\u72af\u4e86\u75c5\u4e00\u6837\u7684\uff0c\u4efb\u4f55\u9879\u76ee\uff0c\u4e0d\u7ba1\u5927\u5c0f\u5168\u90e8\u90fd\u662f\u9996\u5148\u5f15\u5165jQuery\u3002\n<\/p>\n<p>\n\t\u8fd9\u6837\u7684\u602a\u73b0\u8c61\u5728\u4eca\u540e\u53ef\u80fd\u4f1a\u9010\u6e10\u51cf\u5c11\uff0cw3c\u6b63\u5728\u52aa\u529b\u4eba\u8ba9\u95e8\u51cf\u5c11\u5bf9jQuery\u7684\u4f9d\u8d56\u3002\u56e0\u6b64\u5728HTML5\u8bde\u751f\u4f9d\u8d56\uff0c\u7c7b\u4f3cjQuery\u4e00\u6837\u7684\u7280\u5229\u7684\u67e5\u8be2\u904d\u5386\u5143\u7d20\u7684\u65b9\u5f0f\u4fbf\u88ab\u63a8\u51fa\u4e86\u3002\n<\/p>\n<p>\n\tHTML5\u4e0d\u4ec5\u4ec5\u662f\u5f15\u5165\u4e86\u4e00\u4e9b\u6807\u7b7e\uff0c\u8fd8\u6709\u4e00\u4e9b\u65b0\u7684JavaScript api\uff0c\u5176\u4e2d\u5c31\u5305\u62ecdocument.querySelectorAll\u3002\u672c\u6587\u5c31\u6765\u7b80\u5355\u7684\u8bf4\u4e0bdocument.querySelectorAll\u7684\u7528\u6cd5\u3002\n<\/p>\n<p>\n\t\u5728HTML5\u4e4b\u524d\uff0c\u901a\u5e38\u6211\u4eec\u53ea\u80fd\u4f7f\u7528\u4e0b\u97624\u4e2aJavaScript\u7684\u65b9\u6cd5\u67e5\u627e\u5143\u7d20\uff1a\n<\/p>\n<ul>\n<li>\n\t\tdocument.getElementById\n\t<\/li>\n<li>\n\t\tdocument.getElementsByClass\n\t<\/li>\n<li>\n\t\tdocument.getElementsByName\n\t<\/li>\n<li>\n\t\tdocument.getElementsByTagName\n\t<\/li>\n<\/ul>\n<p>\n\tHTML5\u53c8\u5728\u4e0a\u9762\u7684\u57fa\u7840\u4e0a\u63a8\u51fa\u4e86\u4e24\u4e2a\u65b0\u7684\u65b9\u6cd5\uff1a\n<\/p>\n<ul>\n<li>\n\t\tdocument.querySelector\n\t<\/li>\n<li>\n\t\tdocument.querySelectorAll\n\t<\/li>\n<\/ul>\n<p>\n\t\u73b0\u5728\u6211\u4eec\u5c31\u53ef\u4ee5\u4e0d\u4f7f\u7528jQuery\uff0c\u4f46\u662f\u8fd8\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528$\u7f8e\u5143\u7b26\u53f7\u6765\u64cd\u4f5cDOM\u3002\u4e0b\u9762\u6211\u5c31\u5bf9document.querySelectorAll\u8fdb\u884c\u4e00\u4e0b\u5c01\u88c5\u3002\n<\/p>\n<pre class=\"brush:jscript;\">\r\nvar $ = document.querySelectorAll.bind(document);\r\n\/\/\u7136\u540e\u6211\u4eec\u9009\u62e9dom\u65f6\uff0c\u53ef\u4ee5\u8fdb\u884c\u4e0b\u9762\u7684\u64cd\u4f5c\r\nconsole.debug($(&#039;body&#039;));<\/pre>\n<p>\n\t\u6709\u7f51\u53cb\u53d1\u73b0\u4e3a\u4f55\u4e0d\u76f4\u63a5\u4f7f\u7528var $ = document.querySelectorAll;\u5462\uff1f\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"document.querySelectorAll\" src=\"http:\/\/ocuzluml6.bkt.clouddn.com\/20170816001.png\" style=\"width: 560px;height: 426px\" \/>\n<\/p>\n<p>\n\t\u76f4\u63a5\u4f7f\u7528\u4f1a\u62a5\u9519\uff0c\u62a5\u9519\u539f\u56e0\u662f querySelectorAll \u6240\u9700\u7684\u6267\u884c\u4e0a\u4e0b\u6587\u5fc5\u9700\u662f document\uff0c\u800c\u6211\u4eec\u8d4b\u503c\u5230 $ \u8c03\u7528\u540e\u4e0a\u4e0b\u6587\u53d8\u6210\u4e86\u5168\u5c40 window\u3002\n<\/p>\n<p>\n\t\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u53ef\u4ee5\u8fdb\u884c\u5c01\u88c5\uff0c\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nvar query = document.querySelector.bind(document);\r\nvar queryAll = document.querySelectorAll.bind(document);\r\nvar fromId = document.getElementById.bind(document);\r\nvar fromClass = document.getElementsByClassName.bind(document);\r\nvar fromTag = document.getElementsByTagName.bind(document);<\/pre>\n<p>\n\t\u4e0a\u9762\u8fd9\u4e9b\u83b7\u5f97\u7684\u90fd\u4e0d\u662f\u6570\u7ec4\uff0c\u90fd\u662f\u5355\u4e2a\u5143\u7d20\u8282\u70b9\uff0c\u5982\u679c\u60f3\u8fd4\u56de\u4e00\u4e2aNode List\uff0c\u5219\u9700\u8981\u6309\u4e0b\u9762\u8fdb\u884c\u6269\u5c55\u3002\n<\/p>\n<pre class=\"brush:jscript;\">\r\nArray.prototype.map.call(document.querySelectorAll(&#039;button&#039;),\r\n\tfunction(element,index){\r\n\t\telement.onclick = function(){\r\n\t\t}\r\n});<\/pre>\n<p>\n\t\u4ee5\u4e0a\u5c31\u662f\u5173\u4e8edocument.querySelectorAll\u7684\u4e00\u4e9b\u4f7f\u7528\u603b\u7ed3\uff0c\u6b22\u8fce\u5927\u5bb6\u7559\u8a00\u4ea4\u6d41\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=1789\">HTML5\u7684document.querySelectorAll\u4f7f\u7528\u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u66fe\u51e0\u4f55\u65f6\uff0cjQuery\u4e00\u5ea6\u662f\u6211\u4eec\u4f7f\u7528\u7387\u6700\u9ad8\u7684\u7b2c\u4e09\u65b9\u6846\u67b6\u4e86\u3002\u4eba\u4eec\u548c\u72af\u4e86\u75c5\u4e00\u6837\u7684\uff0c\u4efb\u4f55\u9879\u76ee\uff0c\u4e0d\u7ba1\u5927\u5c0f\u5168\u90e8\u90fd\u662f\u9996\u5148\u5f15 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[119,1106,49],"class_list":["post-1789","post","type-post","status-publish","format-standard","hentry","category-html5","tag-html5","tag-javascript","tag-jquery"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1789","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1789"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1789\/revisions"}],"predecessor-version":[{"id":1790,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1789\/revisions\/1790"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}