{"id":10171,"date":"2021-11-02T23:00:08","date_gmt":"2021-11-02T15:00:08","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10171"},"modified":"2021-11-02T23:00:08","modified_gmt":"2021-11-02T15:00:08","slug":"currentscript-currenttarget-currentsrc","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2021\/11\/currentscript-currenttarget-currentsrc\/","title":{"rendered":"\u89c1\u591a\u8bc6\u5e7f\uff0c\u4ecb\u7ecdWeb\u5f00\u53d1\u4e2dcurrent\u5f00\u5934\u7684\u4e00\u4e9bAPI\u5c5e\u6027"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10171\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=10171<\/a> \u946b\u7a7a\u95f4-\u946b\u751f\u6d3b<br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202111-current.jpg';if(!\/zhangxinxu\/.test(location.host)){document.body.addEventListener('mousedown',function(){location.href='https:\/\/www.zhangxinxu.com\/wordpress\/?p=10171'});}<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202111\/current-cover.jpg\" width=\"480\" height=\"317\" alt=\"\u5c01\u9762\u5360\u4f4d\u56fe\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u4e8b\u4ef6\u5bf9\u8c61currentTarget\u5c5e\u6027<\/h3>\n<p>\u4f7f\u7528\u793a\u610f\uff1a<\/p>\n<pre>button.addEventListsner('click', function (event) {\r\n    \/\/ event.currentTarget\r\n});<\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c<code>currentTarget<\/code> \u662f event \u4e8b\u4ef6\u5bf9\u8c61\u7684\u4e00\u4e2a\u5c5e\u6027\uff0c\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u66f4\u591a\u7684\u662f <code>event.target<\/code>\uff0c\u90a3\u4e48 <code>event.currentTarget<\/code> \u548c <code>event.target<\/code> \u7684\u533a\u522b\u662f\u4ec0\u4e48\u5462\uff1f<\/p>\n<p>\u6211\u4eec\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff0cHTML \u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;a href=\"###\" id=\"element\"&gt;&lt;img src=\"zxx.jpg\"&gt;&lt;\/a&gt;<\/pre>\n<p>\u6b64\u65f6\u7ed9\u5916\u9762\u7684 <code>&lt;a&gt;<\/code> \u5143\u7d20\u7ed1\u5b9a\u70b9\u51fb\u4e8b\u60c5\uff1a<\/p>\n<pre>element.addEventListener('click', function (event) {\r\n    event.target.style.border = '10px solid deeppink';\r\n    event.currentTarget.style.border = '10px solid deepskyblue';\r\n});<\/pre>\n<p>\u5c31\u4f1a\u770b\u5230\uff0c\u5f53\u70b9\u51fb\u56fe\u7247\u7684\u65f6\u5019\uff0c<code>&lt;img&gt;<\/code> \u5143\u7d20\u51fa\u73b0\u4e86\u7c89\u7ea2\u8fb9\u6846\uff0c\u800c <code>&lt;a&gt;<\/code> \u5143\u7d20\u5916\u9762\u51fa\u73b0\u4e86\u5929\u84dd\u8fb9\u6846\u3002<\/p>\n<p>\u5b9e\u65f6\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n<p><a href=\"javascript:\" id=\"element\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/study\/s\/s512\/mm9s.jpg\" width=\"256\" height=\"256\"><\/a><\/p>\n<p><script>element.addEventListener('click', function (event) {event.target.style.border = '10px solid deeppink';event.currentTarget.style.border = '10px solid deepskyblue';});<\/script><\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a\uff1a<\/p>\n<ul>\n<li><code>event.target<\/code> \u8868\u793a\u70b9\u51fb\u7684\u5143\u7d20\uff1b<\/li>\n<li><code>event.currentTarget<\/code> \u8868\u793a\u7ed1\u5b9a\u4e8b\u4ef6\u7684\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p>\u5728\u4e8b\u4ef6\u51fd\u6570\u4e2d\uff0c<code>event.currentTarget<\/code> \u662f\u53ef\u4ee5\u548c <code>this<\/code> \u5212\u7b49\u53f7\u7684\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>button.addEventListsner('click', function (event) {\r\n    \/\/ \u4e0b\u9762\u7684\u8fd4\u56de\u503c\u662f true\r\n    console.log(event.currentTarget === this);\r\n});<\/pre>\n<p>\u8fd9\u5c31\u4f1a\u5e26\u6765\u7591\u95ee\uff0c\u65e2\u7136\u5df2\u7ecf\u6709\u4e86 <code>this<\/code>\uff0c\u90a3\u8fd8\u9700\u8981 <code>currentTarget<\/code> \u5c5e\u6027\u505a\u4ec0\u4e48\u5462\uff1f<\/p>\n<p>\u539f\u56e0\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u56e0\u4e3a <code>this<\/code> \u4e0a\u4e0b\u6587\u662f\u4f1a\u53d8\u5316\u7684\uff0c\u4f8b\u5982\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684 <code>this<\/code> \u5c31\u4e0d\u662f\u7ed1\u5b9a\u4e8b\u4ef6\u7684\u5143\u7d20\uff0c\u6b64\u65f6 <code>currentTarget<\/code> \u5c5e\u6027\u5c31\u80fd\u4f53\u73b0\u5176\u4f5c\u7528\u4e86\uff1a<\/p>\n<pre>button.addEventListsner('click', (event) =&gt; {\r\n    \/\/ \u4e0b\u9762\u7684\u8fd4\u56de\u503c\u662f false\r\n    console.log(event.currentTarget === this);\r\n});<\/pre>\n<h3>\u4e8c\u3001currentScript\u4e0e\u5f53\u524d\u8fd0\u884c\u811a\u672c\u5143\u7d20<\/h3>\n<p><code>currentScript<\/code> \u662f <code>document<\/code> \u5bf9\u8c61\u4e0a\u7684\u4e00\u4e2a\u5c5e\u6027\uff0c\u53ef\u4ee5\u8fd4\u56de\u5f53\u524d JS \u4ee3\u7801\u6267\u884c\u6240\u5c5e\u7684 <code>&lt;script&gt;<\/code> \u5143\u7d20\u3002<\/p>\n<p><script id=\"thatsMe\">console.log(document.currentScript);<\/script><\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>&lt;script id=\"thatsMe\"&gt;console.log(document.currentScript);&lt;\/script&gt;<\/pre>\n<p>\u6b64\u65f6\uff0c\u63a7\u5236\u53f0\u5c31\u4f1a\u6709\u7c7b\u4f3c\u4e0b\u56fe\u7684\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202111\/2021-11-01_185732.png\" width=\"498\" height=\"138\" alt=\"\u5f53\u524d\u811a\u672c\u6267\u884c\u6240\u5728\u7684\u5143\u7d20\"> <\/p>\n<p>\u5927\u5bb6\u5982\u679c\u6761\u4ef6\u5141\u8bb8\uff08\u4f8b\u5982\u684c\u9762\u6d4f\u89c8\u5668\u8bbf\u95ee\uff09\uff0c\u6253\u5f00\u63a7\u5236\u53f0\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u622a\u56fe\u6240\u793a\u7684\u5185\u5bb9\u8f93\u51fa\u3002<\/p>\n<p>\u53c8\u4f8b\u5982\uff1a<\/p>\n<p><script src=\".\/zxx.js\"><\/script><\/p>\n<pre>&lt;script src=\".\/zxx.js\"&gt;&lt;\/script&gt;<\/pre>\n<p>\u5176\u4e2d\uff0czxx.js \u4e2d\u6709\u8fd9\u4e48\u4e00\u884c JS \u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&gt;console.log(document.currentScript);<\/pre>\n<p>\u5c31\u4f1a\u8f93\u51fa\u4e0b\u9762\u622a\u56fe\u6240\u793a\u7684\u6548\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202111\/2021-11-01_191715.png\" width=\"289\" height=\"119\" alt=\"\u5916\u9732 JS \u4e0ecurrentScript\"><\/p>\n<h4>\u6ce8\u610f\u4e8b\u9879<\/h4>\n<p><code>document.currentScript<\/code> \u4e0d\u80fd\u5728\u4e8b\u4ef6\u4e2d\uff0c\u6216\u8005 JS \u5df2\u7ecf\u52a0\u8f7d\u5b8c\u6bd5\u4e4b\u540e\u518d\u6267\u884c\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>document.addEventListener('click', function () {\r\n    console.log(document.currentScript);\r\n});<\/pre>\n<p>\u6b64\u65f6\uff0c\u5728\u63a7\u5236\u53f0\u8f93\u51fa\u7684 log \u503c\u5c31\u662f <code>null<\/code>\u3002<\/p>\n<p>\u53c8\u4f8b\u5982\u5982\u679c\u8bbe\u7f6e\u4e86 <code>type=\"module\"<\/code>\uff0c\u8f93\u51fa\u7ed3\u679c\u4e5f\u662f <code>null<\/code>\u3002<\/p>\n<pre>&lt;script id=\"thatsMe\" type=\"module\"&gt;\r\n    \/\/ \u7ed3\u679c\u662f null\r\n    console.log(document.currentScript);\r\n&lt;\/script&gt;<\/pre>\n<p>\u53ef\u89c1 <code>document.currentScript<\/code> \u8fd9\u4e2a\u5c5e\u6027\u5f00\u59cb\u633a\u8106\u7684\uff0c\u5c5e\u4e8e\u4e8b\u513f\u591a\u6d3b\u8fd8\u4e0d\u600e\u4e48\u6837\u7684\u5c5e\u6027\u3002<\/p>\n<h4>\u4f7f\u7528\u573a\u666f<\/h4>\n<p><code>document.currentScript<\/code> \u662f\u4e2a\u4f4e\u9891\u4f7f\u7528\u5c5e\u6027\uff0c\u5f53\u4e00\u6bb5 JS \u6216\u4e00\u4e2a JS \u6a21\u5757\u88ab\u591a\u5904\u5f15\u7528\u7684\u65f6\u5019\uff0c<code>document.currentScript<\/code> \u53ef\u4ee5\u5e2e\u5fd9\u5224\u65ad\u5f15\u7528\u6765\u6e90\uff0c\u548c\u6240\u5904\u7684\u4e0a\u4e0b\u6587\u73af\u5883\u3002<\/p>\n<p>\u4f8b\u5982 A \u9875\u9762\u548c B \u9875\u9762\u540c\u65f6\u5f15\u7528\u4e86 <code>zxx.js<\/code> \u6587\u4ef6\uff0c\u6b64\u65f6\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 <code>document.currentScript<\/code> \u5224\u65ad\u4f7f\u7528\u5f53\u524d JS \u7684\u662f\u54ea\u4e2a\u9875\u9762\u3002<\/p>\n<h4>\u517c\u5bb9\u6027<\/h4>\n<p>IE12+\u6d4f\u89c8\u5668\u652f\u6301\uff0c\u5982\u4e0b\u622a\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202111\/2021-11-01_194623.png\" width=\"630\" height=\"302\" alt=\"\"><\/p>\n<p>IE6-IE9\uff0c\u4ee5\u53caIE11\u6d4f\u89c8\u5668\u6709\u5bf9\u5e94\u7684 polyfill \u89e3\u51b3\u65b9\u6cd5\uff0c\u9879\u76ee\u5730\u5740\u53c2\u89c1\uff1a<a href=\"https:\/\/github.com\/JamesMGreene\/document.currentScript\" target=\"_blank\" rel=\"nofollow noopener\">https:\/\/github.com\/JamesMGreene\/document.currentScript<\/a><\/p>\n<h3>\u4e09\u3001Web Animation API\u4e4bcurrentTime<\/h3>\n<p>Web Animation API \u5728\u51e0\u5e74\u524d\u6211\u6709\u4e13\u95e8\u4ecb\u7ecd\u8fc7\uff0c\u6233<a href=\"zhangxinxu.com\/wordpress\/2018\/03\/web-animations-api-dynamic-feature-animation\/\">\u8fd9\u91cc\u4e86\u89e3\u8be6\u7ec6<\/a>\u3002<\/p>\n<p><code>currentTime<\/code> \u8868\u793a\u5f53\u524d\u52a8\u753b\u6267\u884c\u7684\u65f6\u95f4\uff0c\u4f8b\u5982\u4e00\u4e2a\u52a8\u753b\u603b\u8ba1 3s\uff0c\u5219 <code>currentTime<\/code> \u53ef\u4ee5\u8fd4\u56de\u5f53\u524d\u6267\u884c\u7684\u65f6\u95f4\u662f\u5728\u54ea\u91cc\uff0c\u5355\u4f4d\u662f ms\uff0c\u4e5f\u5c31\u662f\u6beb\u79d2\u3002<\/p>\n<p>\u4f7f\u7528\u793a\u610f\uff1a<\/p>\n<pre>var fadeAni = element.animate([\r\n    { opacity: 0 },\r\n    { opacity: 1 }\r\n], 3000);\r\nsetTimeout(() =&gt; {\r\n    console.log(fadeAni.currentTime);\r\n}, 1500);<\/pre>\n<p>\u6b64\u65f6\u663e\u793a\u7684\u65f6\u95f4\u6bd4 1483 \u7565\u9ad8\u4e00\u70b9\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202111\/2021-11-02_102711.png\" width=\"370\" height=\"246\" alt=\"\u52a8\u753b\u5f53\u524d\u65f6\u95f4\"><\/p>\n<p>\u51fa\u4e4e\u610f\u6599\u7684\u4e0d\u662f1500\uff0c\u6211\u5c1d\u8bd5\u4e86\u51e0\u6b21\uff0c\u65f6\u95f4\u90fd\u662f\u5dee\u4e0d\u591a\u7684\uff0c\u90fd\u662f 1483ms \u5927\u4e00\u70b9\u3002<\/p>\n<p>\u5728Firefox\u6d4f\u89c8\u5668\u4e0b\u7ed3\u679c\u662f\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202111\/2021-11-02_104710.png\" width=\"343\" height=\"251\" alt=\"Firefox\u6267\u884c\u65f6\u95f4\"><\/p>\n<p>\u55ef\u4e48\u2026\u2026Chrome \u6d4f\u89c8\u5668\u7684\u503c\u8fd8\u53ef\u4ee5\u7406\u89e3\uff0c\u52a8\u753b\u7684\u5237\u65b0\u7387\u662f 60\uff0c\u6bcf\u6b21\u5237\u65b0\u95f4\u9694\u65f6\u95f4\u662f 16.666ms\uff0c\u5b9a\u65f6\u5668\u6267\u884c\u7684\u65f6\u5019\u8fd8\u5728\u524d\u9762\u4e00\u5e27\uff0c1500 &#8211; 16.6666\uff0c\u65f6\u95f4\u6b63\u597d\u5bf9\u5f97\u4e0a\u3002<\/p>\n<p>\u81f3\u4e8e Firefox \u6d4f\u89c8\u5668\uff0c\u641e\u4e0d\u5b9a\uff0c\u7b97\u4e86\uff0c\u4ee5\u540e\u6709\u65f6\u95f4\u518d\u6df1\u7a76\u5427\u3002<\/p>\n<h4>\u517c\u5bb9\u6027<\/h4>\n<p>Web Animations API \u7684\u517c\u5bb9\u6027\u4e3b\u8981\u662f Safari \u6d4f\u89c8\u5668\u62d6\u4e86\u540e\u817f\uff0c\u4e0d\u8fc7\u6709\u6210\u719f\u7684 polyfill \u4ee3\u7801\uff0c\u56e0\u6b64\uff0c\u5b9e\u9645\u5f00\u53d1\u8fd8\u662f\u4f7f\u7528\u7684\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202111\/2021-11-02_162115.png\" width=\"553\" height=\"381\" alt=\"Web Animations\u517c\u5bb9\u6027\u622a\u56fe\"><\/p>\n<p><span class=\"hidden\">\/\/zxx: \u5982\u679c\u4f60\u770b\u5230\u8fd9\u6bb5\u6587\u5b57\uff0c\u8bf4\u660e\u4f60\u73b0\u5728\u8bbf\u95ee\u662f\u4e0d\u662f\u539f\u6587\u7ad9\u70b9\uff0c\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u5728\u8fd9\u91cc\uff1ahttps:\/\/www.zhangxinxu.com\/wordpress\/?p=10171\uff08\u4f5c\u8005\u5f20\u946b\u65ed\uff09<\/span><\/p>\n<h3>\u56db\u3001TreeWalker API\u4e2d\u7684currentNode<\/h3>\n<p>\u5173\u4e8e TreeWalker API \u6211\u4e0d\u60f3\u591a\u8bf4\uff0c\u56e0\u4e3a\uff0c\u65e5\u5e38\u5f00\u53d1\u4f7f\u7528 TreeWalker API \u7684\u673a\u4f1a\u5e76\u4e0d\u5927\uff0c\u5982\u679c\u4f60\u662f\u5f00\u53d1\u5927\u578b\u7684\u6846\u67b6\u6216\u8005\u5e93\uff0c\u9700\u8981\u5bf9\u6574\u4e2a DOM \u6587\u6863\u7684\u6811\u5f62\u7ed3\u6784\u8fdb\u884c\u904d\u5386\u5904\u7406\uff0c\u5219 TreeWalker API \u5c31\u6bd4\u8f83\u5408\u9002\u3002<\/p>\n<p>\u5982\u679c\u53ea\u662f\u666e\u901a\u7684\u904d\u5386\uff0c\u5219 <code>document.querySelectorAll()<\/code> \u5c31\u80fd\u6ee1\u8db3\u6211\u4eec\u7684\u65e5\u5e38\u5f00\u53d1\u9700\u6c42\u3002<\/p>\n<p>\u4e0e TreeWalker API \u76f8\u7c7b\u4f3c\u7684\u8fd8\u6709 NodeIterator API \u7684\uff0c\u5c31\u529f\u80fd\u800c\u8a00\uff0c NodeIterator API \u66f4\u7b80\u5355\uff0c\u66f4\u7eaf\u7cb9\u4e00\u70b9\u3002<\/p>\n<p>\u800c TreeWalker \u5219\u7ec6\u8282\u548c\u529f\u80fd\u4e0a\u8981\u66f4\u5f3a\u5927\u4e00\u4e9b\uff0c\u5305\u62ec\uff0c\u53ef\u4ee5\u5ffd\u7565\u6574\u4e2a\u8282\u70b9\u8fc7\u6ee4\uff08NodeFilter.FILTER_REJECT\uff09\uff0c\u53ef\u4ee5\u6307\u5b9a\u8fc7\u6ee4\u7684\u65b9\u5411\uff0c\u652f\u6301\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u4e5f\u66f4\u591a\uff0c\u4f8b\u5982\u8fd9\u91cc\u8981\u4ecb\u7ecd\u7684 <code>currentNode<\/code>\u3002<\/p>\n<p>\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u8bf4\u8bf4\u4e24\u8005\u7684\u533a\u522b\u3002<\/p>\n<p>\u5df2\u77e5 HTML \u5982\u4e0b\uff1a<\/p>\n<pre>&lt;ul id=\"container\"&gt;\r\n    &lt;li&gt;\u5217\u8868&lt;\/li&gt;\r\n    &lt;li&gt;\r\n        LI \u5185\u5d4c\u5217\u8868\uff1a\r\n        &lt;ol&gt;\r\n            &lt;li&gt;\u9879\u76ee1&lt;\/li&gt;\r\n            &lt;li&gt;\u9879\u76ee2&lt;\/li&gt;\r\n        &lt;\/ol&gt;\r\n    &lt;\/li&gt;\r\n&lt;\/ul&gt;<\/pre>\n<h4>1. FILTER_REJECT \u4f5c\u7528\u533a\u522b<\/h4>\n<p>NodeIterator \u4e2d\u867d\u7136\u4e5f\u53ef\u4ee5\u4f7f\u7528 NodeFilter.FILTER_REJECT\uff0c\u4f46\u662f\u53ea\u662f\u8bed\u6cd5\u4e0a\u652f\u6301\uff0c\u5b9e\u9645\u6548\u679c\u548c NodeFilter.FILTER_RSKIP \u7c7b\u4f3c\uff0c\u4f46\u662f\u5728 TreeWalker \u4e2d\uff0c \u8282\u70b9\u4e00\u65e6\u5339\u914d NodeFilter.FILTER_REJECT\uff0c\u5176\u6240\u6709\u7684\u5b50\u5143\u7d20\u90fd\u4f1a\u88ab\u5ffd\u7565\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>var nodeIterator = document.createNodeIterator(container, NodeFilter.SHOW_ELEMENT, { \r\n    acceptNode: function(node) { \r\n        return node.nodeName == 'LI' ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT; \r\n    } \r\n});\r\n\r\nvar pars = [];\r\nvar currentNode;\r\n\r\nwhile (currentNode = nodeIterator.nextNode()) {\r\n    pars.push(currentNode);\r\n}\r\n\r\nconsole.log(pars);<\/pre>\n<pre>var treeWalker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, { \r\n    acceptNode: function(node) { \r\n        return node.nodeName == 'LI' ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT; \r\n    }\r\n}, false);\r\n\r\nvar nodeList = [];\r\nvar currentNode2;\r\n\r\nwhile (currentNode2 = treeWalker.nextNode()) {\r\n    nodeList.push(currentNode2);\r\n}\r\n\r\nconsole.log(nodeList);<\/pre>\n<p>\u4e0a\u9762\u4e24\u6bb5 JS \u4ee3\u7801\u51e0\u4e4e\u4e00\u81f4\uff0c\u533a\u522b\u5c31\u5728\u4e8e\u4e00\u4e2a\u662f <code>createNodeIterator<\/code>\uff0c\u4e00\u4e2a\u662f <code>createTreeWalker<\/code>\uff0c\u4f46\u662f\u6700\u7ec8\u51fa\u4e66\u7684\u7ed3\u679c\u5374\u660e\u663e\u6709\u533a\u522b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202111\/2021-11-02_151535.png\" width=\"256\" height=\"121\" alt=\"\u5bf9\u6bd4\u8f93\u51fa\u7ed3\u679c\"><\/p>\n<h4>2. \u5c5e\u6027\u548c\u65b9\u6cd5\u6570\u91cf\u7684\u533a\u522b<\/h4>\n<p>\u76f4\u63a5\u770b\u4e0b\u9762\u7684\u8868\uff0c\u663e\u793a\u4e86 NodeIterator \u548c TreeWalker \u5bf9\u8c61\u5728\u652f\u6301\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u4e0a\u7684\u533a\u522b\uff1a<\/p>\n<table class=\"params_table\" width=\"300\" style=\"border-spacing:1px\">\n<thead>\n<tr>\n<th width=\"150\">TreeWalker\u5c5e\u6027<\/th>\n<th>NodeIterator\u5c5e\u6027<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>filter<\/td>\n<td>filter<\/td>\n<\/tr>\n<tr>\n<td>currentNode<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"params_table\" width=\"300\" style=\"border-spacing:1px\">\n<thead>\n<tr>\n<th width=\"150\">TreeWalker\u65b9\u6cd5<\/th>\n<th>NodeIterator\u65b9\u6cd5<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>previousNode()<\/td>\n<td>previousNode()<\/td>\n<\/tr>\n<tr>\n<td>nextNode()<\/td>\n<td>nextNode()<\/td>\n<\/tr>\n<tr>\n<td>parentNode()<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>firstChild()<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>lastChild()<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>previousSibling()<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>nextSibling()<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u53ef\u89c1 TreeWalker \u7684\u904d\u5386\u8981\u5f3a\u5927\u5f88\u591a\u3002<\/p>\n<h4>3. currentNode \u7684\u542b\u4e49\u548c\u4f5c\u7528<\/h4>\n<p><code>TreeWalker.currentNode<\/code> \u53ef\u4ee5\u8fd4\u56de <code>TreeWalker<\/code> \u5bf9\u8c61\u5f53\u524d\u6307\u5b9a\u7684\u8282\u70b9\u3002<\/p>\n<p>\u4f8b\u5982\u4e0b\u9762\u7684\u793a\u610f\u4ee3\u7801\uff1a<\/p>\n<pre>var treeWalker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, { \r\n    acceptNode: function(node) { \r\n        return node.nodeName == 'LI' ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT; \r\n    }\r\n}, false);\r\n\r\nvar nodeList = [];\r\nvar currentNode = treeWalker.currentNode;\r\n\r\nwhile (currentNode) {\r\n    nodeList.push(currentNode);\r\n    currentNode = treeWalker.nextNode();\r\n}\r\n\r\nconsole.log(nodeList);<\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\u622a\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202111\/2021-11-02_154417.png\" width=\"315\" height=\"196\" alt=\"currentNode\"><\/p>\n<p><code>treeWalker.currentNode<\/code> \u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\u4e00\u5b9a\u662f\u6839\u8282\u70b9\u3002<\/p>\n<p>\u5728\u5f00\u53d1\u4e2d\u7684\u4f5c\u7528\uff0c\u5f80\u5f80\u662f\u6539\u53d8\u904d\u5386\u7684\u6839\u8282\u70b9\u4f4d\u7f6e\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>treeWalker.currentNode = otherNode;<\/pre>\n<p>\u6b64\u65f6\uff0c\u904d\u5386\u7684\u8d77\u70b9\u4f1a\u6539\u53d8\u4e3a otherNode\u3002<\/p>\n<p>TreeWalker API \u662f\u4e00\u4e2a IE9+ \u6d4f\u89c8\u5668\u652f\u6301\u7684\u6210\u719f\u7684 API\uff0c\u53ef\u4ee5\u653e\u5fc3\u4f7f\u7528\u3002<\/p>\n<h3>\u4e94\u3001IMG \u56fe\u7247\u5f53\u524d\u5730\u5740 currentSrc<\/h3>\n<p>\u76f8\u6bd4\u4e0a\u9762\u51e0\u4e2a\u5c5e\u6027\uff0c<code>currentSrc<\/code> \u5c5e\u6027\u8981\u66f4\u5e38\u7528\uff0c\u4e5f\u66f4\u5b9e\u7528\u4e9b\uff0c\u56e0\u4e3a\u53ef\u66ff\u4ee3\u4f4e\u3002<\/p>\n<p><code>currentSrc<\/code> \u4e3b\u8981\u7528\u5728\u54cd\u5e94\u5f0f\u56fe\u7247\u5f00\u53d1\u4e2d\u3002<\/p>\n<p>\u6240\u8c13\u201c\u54cd\u5e94\u5f0f\u56fe\u7247\u201d\u53ef\u4ee5\u53c2\u8003\u6211\u4e4b\u524d\u7684\u8fd9\u7bc7\u6587\u7ae0\uff1a\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2014\/10\/responsive-images-srcset-size-w-descriptor\/\">\u54cd\u5e94\u5f0f\u56fe\u7247srcset\u5168\u65b0\u91ca\u4e49sizes\u5c5e\u6027w\u63cf\u8ff0\u7b26<\/a>\u201d\u3002<\/p>\n<p>\u4f8b\u5982\u4e0b\u9762\u8fd9\u6837\u4e00\u4e2a HTML \u4ee3\u7801\uff1a<\/p>\n<pre>&lt;img src=\"1.jpg\"\r\n  srcset=\"1.jpg 128w, 2.jpg 256w, 3.jpg 512w\"\r\n  sizes=\"(max-width: 360px) 340px, 128px\"&gt;<\/pre>\n<p>\u5728\u4e0d\u540c\u7684\u8bbe\u5907\u5bbd\u5ea6\u548c\u4e0d\u540c\u7684\u8bbe\u5907\u5bc6\u5ea6\u4e0b\uff0c\u52a0\u8f7d\u7684\u56fe\u7247\u662f\u4f1a\u6709\u533a\u522b\u7684\uff0c\u4e0b\u9762\u95ee\u9898\u6765\u4e86\uff0c\u5982\u4f55\u77e5\u9053\u5f53\u524d <code>&lt;img&gt;<\/code> \u5143\u7d20\u52a0\u8f7d\u7684\u56fe\u7247\u5462\uff1f<\/p>\n<p>\u8fd9\u5c31\u9700\u8981 <code>currentSrc<\/code> \u5c5e\u6027\uff0c\u8fd4\u56de\u5f53\u524d\u771f\u5b9e\u4ef7\u503c\u7684\u56fe\u7247 URL \u5730\u5740\u3002<\/p>\n<p>\u5982\u679c\u56fe\u7247\u7684\u8d44\u6e90\u52a0\u8f7d\u5224\u65ad\u662f\u4f7f\u7528\u7684 <code>&lt;source&gt;<\/code> \u5143\u7d20\u5b9e\u73b0\u7684\uff0c\u5219 <code>currentSrc<\/code> \u5c5e\u6027\u4e5f\u662f\u6709\u6548\u7684\u3002<\/p>\n<pre>&lt;picture&gt;\r\n    &lt;source srcset=\"logo-768.png 768w, logo-768-1.5x.png 1.5x\"&gt;\r\n    &lt;source srcset=\"logo-480.png, logo-480-2x.png 2x\"&gt;\r\n    &lt;img id=\"img\" src=\"logo-320.png\" alt=\"logo\"&gt;\r\n&lt;\/picture&gt;<\/pre>\n<p>\u6b64\u65f6\uff0c<code>img.currentSrc<\/code> \u5c31\u662f\u771f\u5b9e\u52a0\u8f7d\u7684\u56fe\u50cf URL \u5730\u5740\u3002<\/p>\n<h4>audio\u3001video\u540c\u6837\u9002\u7528<\/h4>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>&lt;video controls&gt;\r\n    &lt;source src=\"foo.webm\" type=\"video\/webm\"&gt;\r\n    &lt;source src=\"foo.ogg\" type=\"video\/ogg\"&gt;\r\n    &lt;source src=\"foo.mov\" type=\"video\/quicktime\"&gt;\r\n&lt;\/video&gt;<\/pre>\n<p>\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528 <code>video.currentSrc<\/code> \u83b7\u77e5\u6d4f\u89c8\u5668\u52a0\u8f7d\u7684\u7a76\u7adf\u662f\u54ea\u4e2a\u8d44\u6e90\u5730\u5740\u3002<\/p>\n<h4>\u517c\u5bb9\u6027<\/h4>\n<p><code>currentSrc<\/code> \u5c5e\u6027\u7684\u517c\u5bb9\u6027\u548c <code>srcset<\/code> \u7b49\u5c5e\u6027\u7c7b\u4f3c\uff0c\u662f\u4e00\u4e2a\u53ef\u4ee5\u5728\u79fb\u52a8\u7aef\u653e\u5fc3\u4f7f\u7528\u7684html\u5c5e\u6027\uff0c\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202111\/2021-11-02_223307.png\" width=\"637\" height=\"231\" alt=\"currentSrc \u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u516d\u3001\u5176\u4ed6\u53ca\u7ed3\u8bed<\/h3>\n<p>\u5bf9\u4e8e SVG \u5143\u7d20\uff0c\u8fd8\u6709 <code>currentScale<\/code> \u548c <code>currentTranslate<\/code> \u8fd9\u4e24\u4e2a\u5c5e\u6027\uff0c\u8868\u793a SVG \u5143\u7d20\u5f53\u524d\u7f29\u653e\u548c\u4f4d\u79fb\uff0c\u4e0d\u8fc7\u56e0\u4e3a\u65e5\u5e38\u5f00\u53d1\u4f7f\u7528\u573a\u666f\u4e0d\u591a\uff0c\u8fd9\u91cc\u4e0d\u505a\u5c55\u5f00\u4ecb\u7ecd\u3002<\/p>\n<p>\u5176\u4ed6\u5c31\u6ca1\u4ec0\u4e48\u4e86\uff0c\u672c\u6587\u4ecb\u7ecd\u7684\u8fd95\u4e2a current \u5f00\u5934\u7684\u5c5e\u6027\uff0c\u867d\u7136\u5927\u5bb6\u53ef\u80fd\u6ca1\u600e\u4e48\u7528\u8fc7\uff0c\u4f46\u5176\u5b9e\u90fd\u662f\u633a\u6709\u7528\u7684\u4e9b\u5c5e\u6027\uff0c\u5efa\u8bae\u4e86\u89e3\u4e0b\uff0c\u53ef\u4ee5\u4e0d\u6682\u65f6\u4e0d\u9700\u8981\u77e5\u9053\u5177\u4f53\u7684\u7ec6\u8282\uff0c\u4f46\u662f\u9700\u8981\u77e5\u9053\u6709\u8fd9\u4e48\u4e2a\u4e1c\u897f\uff0c\u8fd9\u6837\u5f53\u4f60\u9047\u5230\u7c7b\u4f3c\u573a\u666f\u7684\u65f6\u5019\uff0c\u5c31\u80fd\u591f\u5feb\u901f\u627e\u5230\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<p>\u672c\u6587\u5185\u5bb9\u5df2\u7ecf\u5f88\u957f\u4e86\uff0c\u5c31\u4e0d\u518d\u7ee7\u7eed\u5c55\u5f00\u505a\u4ecb\u7ecd\u4e86\u3002<\/p>\n<p>\u884c\u6587\u4ed3\u4fc3\uff0c\u5982\u679c\u6587\u4e2d\u6709\u8868\u8ff0\u4e0d\u51c6\u786e\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u6307\u6b63\u3002<\/p>\n<p>\u5982\u679c\u60a8\u89c9\u5f97\u672c\u6587\u7684\u5185\u5bb9\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff0c\u6b22\u8fce\u8f6c\u53d1\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u6bd4\u5fc3\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/2764.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10171\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=10171<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202111\/current-cover.jpg\" width=\"160\" height=\"106\" alt=\"\u5c01\u9762\u5360\u4f4d\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u4e3b\u8981\u4ecb\u7ecd currentTarget\u3001currentScript\u3001currentTime\u3001currentNode \u548c currentSrc \u8fd95\u4e2a\u5c5e\u6027 API\uff0c\u6709\u4e9b\u662f JS \u5bf9\u8c61\u5c5e\u6027\uff0c\u6709\u4e9b\u662f HTML \u5bf9\u8c61\u5c5e\u6027\uff0c\u90fd\u633a\u6709\u7528\u7684\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1097,1284],"tags":[1727,707,1795,1172,1792,1796,1791,1450,1793,949,1794],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/10171"}],"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=10171"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/10171\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=10171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=10171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=10171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}