{"id":9533,"date":"2020-08-20T21:46:56","date_gmt":"2020-08-20T13:46:56","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9533"},"modified":"2020-08-20T22:04:28","modified_gmt":"2020-08-20T14:04:28","slug":"js-customevent-pass-param","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2020\/08\/js-customevent-pass-param\/","title":{"rendered":"JS CustomEvent\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u4f20\u53c2\u5c0f\u6280\u5de7"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9533\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9533<\/a><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\/202008-custom-event-params.png';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202008\/custom-event-params.png\" width=\"415\" height=\"277\" alt=\"CustomEvent\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u4f20\u53c2\" class=\"alignright clip size-medium\" \/><\/p>\n<p>\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u7684\u4f20\u53c2\u4e3b\u8981\u662f\u901a\u8fc7<code>CustomEvent.detail<\/code>\u8fdb\u884c\u7684\u3002<\/p>\n<h3>\u4e00\u3001addEventListener\u4e8b\u4ef6\u89e6\u53d1<\/h3>\n<p>\u4f8b\u5982\uff0c\u7ed9\u8f93\u5165\u6846\u5143\u7d20\uff08\u5047\u8bbeDOM\u5bf9\u8c61\u53d8\u91cf\u540d\u79f0\u662f<code>input<\/code>\uff09\u7ed1\u5b9a\u4e86\u4e00\u4e2a<code>'input'<\/code>\u4e8b\u4ef6\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>input.addEventListener('input', function () {\r\n    <span class=\"green\">\/\/ write by zhangxinxu<\/span>\r\n});<\/pre>\n<p>\u6b64\u65f6\uff0c\u70b9\u51fb\u4e0b\u62c9\u6846\u5e0c\u671b\u89e6\u53d1\u8f93\u5165\u6846\u5143\u7d20\u7684<code>'input'<\/code>\u4e8b\u4ef6\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528<code>dispatchEvent<\/code>\u65b9\u6cd5\u548c<code>CustomEvent<\/code>\u5bf9\u8c61\u3002<\/p>\n<p>JS\u4ee3\u7801\u5982\u4e0b\u793a\u610f\uff1a<\/p>\n<pre>var myEvent = new CustomEvent('input');\r\ninput.dispatchEvent(myEvent);<\/pre>\n<p>\u4e5f\u53ef\u4ee5\u5408\u5728\u4e00\u8d77\u4e66\u5199\uff0cJS\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>input.dispatchEvent(new CustomEvent('input'));<\/pre>\n<p>\u5982\u679c\u5e0c\u671b\u89e6\u53d1<code>'input'<\/code>\u4e8b\u4ef6\u7684\u540c\u65f6\u4f20\u9012\u4e0b\u62c9\u5217\u8868\u5bf9\u5e94\u7684\u53c2\u6570\u5bf9\u8c61\u7ed9<code>'input'<\/code>\u4e8b\u4ef6\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528<code>CustomEvent.detail<\/code>\u3002<\/p>\n<p>\u8bed\u6cd5\u793a\u610f\u5982\u4e0b\uff1a<\/p>\n<pre>var myEvent = new CustomEvent('input', {\r\n    <span class=\"green\">\/\/ zhangxinxu:\r\n    \/\/ objParams\u5c31\u662f\u9700\u8981\u4f20\u9012\u7684\u53c2\u6570\uff0c\r\n    \/\/ \u53ef\u4ee5\u662f\u4efb\u610f\u7684\u7c7b\u578b<\/span>\r\n    detail: objParams\r\n});\r\ninput.dispatchEvent(myEvent);<\/pre>\n<p><span class=\"hidden\">\/\/zxx: \u5982\u679c\u4f60\u770b\u5230\u8fd9\u6bb5\u6587\u5b57\uff0c\u8bf4\u660e\u4f60\u73b0\u5728\u8bbf\u95ee\u662f\u4f53\u9a8c\u7cdf\u7cd5\u7684\u5783\u573e\u76d7\u7248\u7f51\u7ad9\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95ee\u539f\u6587\u83b7\u5f97\u5f88\u597d\u7684\u4f53\u9a8c\uff1ahttps:\/\/www.zhangxinxu.com\/wordpress\/?p=9533\uff08\u4f5c\u8005\u5f20\u946b\u65ed\uff09<\/span><\/p>\n<h3>\u4e8c\u3001\u652f\u6301\u4efb\u610f\u7684\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u540d\u79f0<\/h3>\n<p>\u4e0d\u4ec5\u662f\u6d4f\u89c8\u5668\u539f\u751f\u7684\u4e8b\u4ef6\uff0c\u5982<code>'click'<\/code>\uff0c<code>'mousedown'<\/code>\uff0c<code>'change'<\/code>\uff0c<code>'mouseover'<\/code>\uff0c<code>'mouseenter'<\/code>\u7b49\u53ef\u4ee5\u89e6\u53d1\uff0c\u4efb\u610f\u7684\u81ea\u5b9a\u4e49\u540d\u79f0\u7684\u4e8b\u4ef6\u4e5f\u662f\u53ef\u4ee5\u89e6\u53d1\u7684\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>document.body.addEventListener('\u6587\u7ae0\u52ff\u76d7', () => { console.log('\u6587\u660e\u3001\u516c\u6b63\u3001\u6cd5\u6cbb\u3001\u8bda\u4fe1'); });\r\n\/\/ \u89e6\u53d1\r\ndocument.body.dispatchEvent(new CustomEvent('\u6587\u7ae0\u52ff\u76d7'));\r\n<\/pre>\n<p>\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202008\/2020-08-20_150323.png\" width=\"662\" height=\"131\" alt=\"\u4efb\u610f\u7684\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u6267\u884c\u6548\u679c\u793a\u610f\"><\/p>\n<p>\u4f20\u53c2\u4e5f\u662f\u4e00\u6837\u7684\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>document.body.addEventListener('show', (event) => { console.log(event.detail); });\r\n\/\/ \u89e6\u53d1\r\nlet myEvent = new CustomEvent('show', {\r\n    detail: {\r\n        username: 'zhangxinxu.com',\r\n        userid: '20200820'\r\n    }\r\n});\r\ndocument.body.dispatchEvent(myEvent);<\/pre>\n<p>\u8fd0\u884c\u540e\u7684\u7ed3\u679c\u53c2\u89c1\u4e0b\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202008\/2020-08-20_152108.png\" width=\"583\" height=\"210\" alt=\"\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u4f20\u53c2\u622a\u56fe\u6548\u679c\"><\/p>\n<h3>\u4e09\u3001IE\u6d4f\u89c8\u5668\u4e0d\u652f\u6301\u600e\u4e48\u529e\uff1f<\/h3>\n<p>IE\u6d4f\u89c8\u5668\u662f\u4e0d\u652f\u6301<code>CustomEvent.detail<\/code>\u7684\uff0cEdge 14+\u624d\u5f00\u59cb\u652f\u6301\uff0c\u600e\u4e48\u529e\uff0c\u96be\u9053IE\u6d4f\u89c8\u5668\u5c31\u4e0d\u80fd\u4f7f\u7528\u7684\u5417\uff1f<\/p>\n<p>\u4e0d\u6025\uff0c\u53ef\u4ee5\u7528\uff0c\u589e\u52a0\u4e00\u6bb5Polyfill\u811a\u672c\u5c31\u53ef\u4ee5\u4e86\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><span class=\"green\">\/**\r\n * CustomEvent constructor polyfill for IE\r\n *\/<\/span>\r\n(function () {\r\n    if (typeof window.CustomEvent === 'function') {\r\n        <span class=\"green\">\/\/ \u5982\u679c\u4e0d\u662fIE<\/span>\r\n        return false;\r\n    }\r\n\r\n    var CustomEvent = function (event, params) {\r\n        params = params || {\r\n            bubbles: false,\r\n            cancelable: false,\r\n            detail: undefined\r\n        };\r\n        var evt = document.createEvent('CustomEvent');\r\n        evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);\r\n        return evt;\r\n    };\r\n\r\n    CustomEvent.prototype = window.Event.prototype;\r\n\r\n    window.CustomEvent = CustomEvent;\r\n})();<\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u590d\u5236\u5230\u9875\u9762\u4e2d\uff0c\u653e\u5728\u5177\u4f53\u7684\u4e1a\u52a1JavaScript\u4ee3\u7801\u7684\u524d\u9762\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u56e0\u4e3a\u6267\u884c\u65f6\u673a\u5148\u540e\u7684\u95ee\u9898\u5bfc\u81f4\u51fabug\u3002<\/p>\n<h3>\u56db\u3001\u7ed3\u8bed<\/h3>\n<p>\u6700\u8fd1\u6ca1\u4e8b\u641c\u81ea\u5df1\u5199\u7684\u6587\u7ae0\uff0c\u7ed3\u679c\u53d1\u73b0\u6bcf\u4e00\u7bc7\u6587\u7ae0\u90fd\u88ab\u91c7\u96c6\u7ad9\u6536\u96c6\u4e86\uff0c\u6ca1\u6709\u539f\u51fa\u5904\uff0c\u4e5f\u4e0d\u7559\u6211\u59d3\u540d\uff0c\u641c\u7d22\u5f15\u64ce\u6392\u540d\u6bd4\u6211\u8fd8\u9ad8\uff0c\u751a\u81f3\u6709\u4e9b\u6587\u7ae0\u67d0\u56fd\u4ea7\u5783\u573e\u641c\u7d22\u5f15\u64ce\u90fd\u6ca1\u6709\u6211\u7684\u540d\u5b57\u3002<\/p>\n<p>\u8fd9\u4e9b\u7f51\u7ad9\u5c45\u7136\u8fd8\u6d3b\u7740\uff0c\u8fd8\u6d3b\u5f97\u5f88\u597d\uff0c\u5c45\u7136\u6392\u540d\u8fd8\u90a3\u4e48\u9ad8\uff0c\u4f8b\u5982\u67d0\u67d0\u4e4b\u5bb6\u4e4b\u7c7b\u7684\u7f51\u7ad9\uff0c\u771f\u7684\u662f\u5f88\u795e\u5947\uff0c\u8fd9\u79cd\u532a\u5937\u6240\u601d\u7684\u73b0\u8c61\u81ea\u7136\u79bb\u4e0d\u5f00\u540c\u6837\u795e\u5947\u7684\u67d0\u5ea6\u3002<\/p>\n<p>2021\u5e74\u5143\u65e6\u4e4b\u540e\uff0c\u65b0\u7684\u6c11\u6cd5\u5178\u6267\u884c\uff0c\u5bf9\u4e8e\u77e5\u8bc6\u4ea7\u6743\u4fb5\u72af\u5df2\u7ecf\u5141\u8bb8\u60e9\u7f5a\u6027\u8d54\u507f\u4e86\uff0c\u5230\u65f6\u5019\uff0c\u6211\u4e00\u5b9a\u76f4\u63a5\u5f8b\u5e08\u51fd\u53d1\u8fc7\u53bb\uff0c\u6211\u5728\u8fd9\u91cc\u7acb\u8d34\u4e3a\u8bc1\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/1f4aa.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=9533\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9533<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202008\/custom-event-params.png\" width=\"160\" height=\"107\" alt=\"CustomEvent\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u4f20\u53c2\" class=\"alignright size-medium\" \/><\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528CustomEvent.detail\u89e6\u53d1addEventListener\u7ed1\u5b9a\u7684\u5185\u7f6e\u6216\u81ea\u5b9a\u4e49\u7684\u4e8b\u4ef6\u5e76\u4f20\u53c2\uff0c\u4ee5\u53caIE\u6d4f\u89c8\u5668\u4e0d\u652f\u6301\u7684\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[705,1653,1652,707,1650,708,1651,13,703],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9533"}],"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=9533"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9533\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=9533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=9533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=9533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}