{"id":1139,"date":"2022-05-07T08:42:33","date_gmt":"2022-05-07T00:42:33","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=1139"},"modified":"2023-02-16T14:43:29","modified_gmt":"2023-02-16T06:43:29","slug":"22-23-24-shi-jian-xiang-guan","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1139","title":{"rendered":"22 &#8211; 23 &#8211; 24 \u4e8b\u4ef6\u76f8\u5173"},"content":{"rendered":"<h2><a id=\"%E4%BA%8B%E4%BB%B6%E5%92%8C%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e8b\u4ef6\u548c\u4e8b\u4ef6\u5904\u7406<\/h2>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/05\/16518842811812.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/events-event-handling-f28\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/events-event-handling-f28<\/a><\/p>\n<h3><a id=\"%E4%BA%8B%E4%BB%B6\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e8b\u4ef6<\/h3>\n<p>\u4f60\u5bf9\u52a8\u4f5c\uff08\u7cfb\u7edf\u7684\u6216\u7528\u6237\u4ea7\u751f\u7684\uff09\u7684\u54cd\u5e94\u5c31\u662f\u8c03\u7528\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u5bf9\u4e8b\u4ef6\u7684\u56de\u5e94\u5c31\u662f\u8c03\u7528\u4e00\u4e2a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u4e00\u4e2a\u6309\u94ae\u540e\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u663e\u793a\u4e00\u4e2a\u5e26\u4fe1\u606f\u7684\u5f39\u6846\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e8b\u4ef6\u662f <strong>click<\/strong> \u5904\u7406\u7ed3\u679c\u5c31\u662f\u5c55\u793a\u4e00\u4e2a\u5f39\u6846\u3002<\/p>\n<p>\u7f51\u9875\u4e0a\u4f1a\u53d1\u751f\u5f88\u591a\u4e8b\u4ef6\uff1a<\/p>\n<ol>\n<li>\u7528\u6237 hover \u4e00\u4e2a\u5143\u7d20\u4e0a<\/li>\n<li>\u8868\u5355\u7684\u63d0\u4ea4<\/li>\n<li>\u89c6\u9891\u505c\u6b62\u64ad\u653e<\/li>\n<li>\u7528\u6237\u4ece\u4e00\u4e2a\u56fe\u7247\u4e0a\u6eda\u52a8\u8fc7\u53bb<\/li>\n<li>\u6539\u53d8\u6d4f\u89c8\u5668\u7684\u5927\u5c0f<\/li>\n<li>\u6309\u952e<\/li>\n<li>\u6587\u6863\u52a0\u8f7d\u7ed3\u675f<\/li>\n<\/ol>\n<h3><a id=\"%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86%E7%A8%8B%E5%BA%8F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f<\/h3>\n<p>\u6211\u4e0a\u9762\u5df2\u7ecf\u7b80\u5355\u63d0\u5230\u8fc7\uff0c\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u5c31\u662f\u6211\u4eec\u5982\u4f55\u54cd\u5e94\u4e8b\u4ef6\u7684\u65b9\u6cd5\u3002\u5b83\u662f\u4e8b\u4ef6\u53d1\u751f\u65f6\u6267\u884c\u7684\u4e00\u5757\u4ee3\u7801\u3002<\/p>\n<p>\u6211\u4eec\u7ecf\u5e38\u4f1a\u628a <strong>event listeners<\/strong> \u548c <strong>evetn handlers<\/strong> \u4ea4\u66ff\u4f7f\u7528\uff0c\u540c\u6837\u4f60\u4e5f\u53ef\u4ee5\u50cf\u8fd9\u6837\u968f\u610f\u4f7f\u7528\u3002<\/p>\n<p>\u53ef\u662f\uff0c\u5b83\u4fe9\u6709\u70b9\u5c0f\u533a\u522b\uff0c<strong>listeners<\/strong> \u662f\u76d1\u542c\u4e00\u4e2a\u4e8b\u4ef6\u7684\u53d1\u751f\u800c <strong>handler<\/strong> \u662f\u6267\u884c\u7684\u5177\u4f53\u4ee3\u7801\u3002<\/p>\n<h3><a id=\"%E6%A1%88%E4%BE%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u6848\u4f8b<\/h3>\n<p>\u5047\u8bbe\u6211\u4eec\u7684\u9875\u9762\u6709\u4e00\u4e2a\u6309\u94ae\u3002<\/p>\n<pre><code class=\"language-plain_text\">&lt;button class=&quot;btn-primary&quot;&gt;Click Me!&lt;\/button&gt;\n<\/code><\/pre>\n<p>\u6211\u4eec\u4e3a\u6309\u94ae\u7ed1\u5b9a\u4e86\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u5f53\u70b9\u51fb\u5b83\u65f6\u6253\u5370\u4e00\u6761\u6d88\u606f\u3002<\/p>\n<pre><code class=\"language-plain_text\">const myButton = document.querySelector(&quot;.btn-primary&quot;);\n\nmyButton.addEventListener(&quot;click&quot;, function() {\n    console.log(&quot;The button was clicked&quot;);\n});\n<\/code><\/pre>\n<p>\u53d1\u751f\u4e86\u4ec0\u4e48\uff1a<\/p>\n<ol>\n<li>\u6211\u4eec\u4f7f\u7528 <strong>querySelector<\/strong>\u83b7\u53d6\u5230\u6d4f\u89c8\u5668 DOM \u4e2d\u7684\u6309\u94ae\uff1b<\/li>\n<li>\u63a5\u7740\u6211\u4eec\u4f7f\u7528 <strong>addEventListener<\/strong> \u6dfb\u52a0\u4e86\u4e8b\u4ef6\u4fa6\u542c\u5668\uff1b<\/li>\n<li><strong>addEventListner<\/strong> \u63a5\u53d7\u4e86\u4e24\u4e2a\u53c2\u6570\uff08\u5b9e\u9645\u53ef\u4ee5\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\uff09\uff1b<\/li>\n<li>\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e8b\u4ef6\u7c7b\u578b\uff0c\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u7684\u4e8b\u4ef6\u7c7b\u578b\u662f click\uff1b<\/li>\n<li>\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5c31\u662f\u4e00\u65e6\u70b9\u51fb\u65f6\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570<\/li>\n<\/ol>\n<p>\u6d4f\u89c8\u5668\u77e5\u9053\u7528\u6237\u4ec0\u4e48\u65f6\u5019\u70b9\u51fb\u4e86\u6309\u94ae\uff0c\u540c\u65f6\u4e3a\u6709\u7c7b\u540d <strong>btn-primary<\/strong> \u7684\u6309\u94ae\u6ce8\u518c\u4e86\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u7136\u540e\u6267\u884c\u76f8\u5173\u7684\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u5c06\u4f1a\u6253\u5370\uff1a<\/p>\n<pre><code class=\"language-plain_text\">The button was clicked\n<\/code><\/pre>\n<p>\u56de\u8c03\u65b9\u6cd5\u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u5b83\u4e0d\u80fd\u88ab\u5176\u5b83\u5730\u65b9\u5f15\u7528\u3002\u6211\u4eec\u4e0d\u7ecf\u5e38\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u547d\u540d\u51fd\u6570\u7136\u540e\u4f20\u9012\u7ed9\u5b83\u3002\u547d\u540d\u51fd\u6570\u662f\u53ef\u91cd\u7528\u6027\u7684\u9996\u9009\uff0c\u5b83\u4f7f\u6211\u4eec\u80fd\u591f\u5728\u4ee5\u540e\u5220\u9664\u4e8b\u4ef6\u4fa6\u542c\u5668\u3002<\/p>\n<h3><a id=\"%E4%BD%BF%E7%94%A8%E5%91%BD%E5%90%8D%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528\u547d\u540d\u51fd\u6570<\/h3>\n<p>\u4e8b\u4ef6\u5904\u7406\u5668\u53ef\u4ee5\u662f\u4e00\u4e2a\u547d\u540d\u51fd\u6570\u3002<\/p>\n<pre><code class=\"language-plain_text\">const myButton = document.querySelector(&quot;.btn-primary&quot;);\n\nconst handleClick = function() {\n    console.log(&quot;The button was clicked&quot;);\n};\n\nmyButton.addEventListener(&quot;click&quot;, handleClick);\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5e76\u4e0d\u4ec5\u4ec5\u662f\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u5b83\u8fd8\u6709\u4e24\u4e2a\u4f18\u70b9\uff1a<\/p>\n<ol>\n<li><strong>\u91cd\u7528\u6027<\/strong>\uff1a\u8bbe\u60f3\u4f60\u6709\u5f88\u591a\u6309\u94ae\u9700\u8981\u6253\u5370\u76f8\u540c\u7684\u8bed\u53e5\uff0c\u4e00\u4e2a\u547d\u540d\u51fd\u6570\u53ef\u4ee5\u88ab\u4f7f\u7528\u591a\u6b21\u800c\u4e0d\u8981\u5199\u91cd\u590d\u7684\u4ee3\u7801\u3002<\/li>\n<li><strong>\u79fb\u51fa\u4e8b\u4ef6\u4fa6\u542c\u5668<\/strong>\uff1a\u4f7f\u7528 <code>removeEventListener<\/code> \u6765\u79fb\u51fa\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u4e3a\u4e86\u79fb\u51fa\u5b83\u9700\u8981\u4f20\u9012\u4e24\u4e2a\u5173\u952e\u53c2\u6570\u3002\u7b2c\u4e00\u4e2a\u662f\u5b9e\u9645\u7c7b\u578b\uff0c\u7b2c\u4e8c\u4e2a\u662f\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u3002\u82e5\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u6211\u4eec\u65e0\u6cd5\u6307\u5b9a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u662f\u547d\u540d\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-plain_text\">myButton.removeEventListener(&quot;click&quot;, handleClick);\n<\/code><\/pre>\n<h2><a id=\"%E4%BA%8B%E4%BB%B6%E5%86%92%E6%B3%A1\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e8b\u4ef6\u5192\u6ce1<\/h2>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/05\/16518843109714.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/event-bubbling-pb3\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/event-bubbling-pb3<\/a><\/p>\n<h3><a id=\"%E7%AE%80%E4%BB%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7b80\u4ecb<\/h3>\n<p>\u4e0a\u9762\u6211\u4eec\u8c08\u4e86\u4e8b\u4ef6\u548c\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u4ee5\u53ca\u4e3a\u4e8b\u4ef6\u6dfb\u52a0\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u3002\u5f53\u4e8b\u4ef6\u53d1\u751f\u65f6\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u5c06\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n<p>JavaScript \u4e2d\u7684\u4e8b\u4ef6\u5192\u6ce1\u662f\u6307\u5f53\u5143\u7d20\u4e0a\u53d1\u751f\u4e00\u4e2a\u4e8b\u4ef6\u65f6\uff0c\u5173\u8054\u7684\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u4f1a\u88ab\u8c03\u7528\uff0c\u7d27\u63a5\u7740\u662f\u7236\u7ea7\u5143\u7d20\u548c\u66f4\u4e0a\u5c42\u5143\u7d20\u7684\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u4e5f\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-plain_text\">&lt;div onclick=&quot;alert('Click Event Happened')&quot;&gt;\n    &lt;p&gt;If you have click this paragraph in the browser, the onclick handler of the div will get invoked.&lt;\/p&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u662f\uff1a\u70b9\u51fb p \u6807\u7b7e\u5185\u7684\u6587\u672c\u65f6\uff0c\u4f1a\u89e6\u53d1 div \u4e0a\u7684 <code>onclick<\/code> \u4e8b\u4ef6\u3002\u8fd9\u5c31\u662f p \u4e0a\u53d1\u751f\u7684\u4e8b\u4ef6\u5192\u6ce1\u5230\u4e86 div \u4e0a\u3002<\/p>\n<p>\u5373\u4f7f\u6709 n  \u591a\u5c42\u5d4c\u5957\u7684\u5143\u7d20\u4e0a\u9762\u7684\u6a21\u5f0f\u4f9d\u65e7\u4e5f\u4f1a\u53d1\u751f\u3002<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-plain_text\">&lt;span onclick=&quot;alert('Span Clicked')&quot;&gt;\n    &lt;div onclick=&quot;alert('Div Clicked')&quot;&gt;\n        &lt;p onclick=&quot;alert('Paragraph Clicked')&quot;&gt;Click Me.&lt;\/p&gt;\n    &lt;\/div&gt;\n&lt;\/span&gt;\n<\/code><\/pre>\n<p>\u82e5\u6211\u4eec\u70b9\u51fb\u4e86 p \u6807\u7b7e\uff0c\u6d4f\u89c8\u5668\u4f1a\u89e6\u53d1\u4e09\u6b21\u5f39\u6846\u3002<\/p>\n<h3><a id=\"%E6%89%BE%E5%88%B0%E4%BA%8B%E4%BB%B6%E7%9A%84%E6%BA%90%E5%A4%B4%E5%85%83%E7%B4%A0\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u627e\u5230\u4e8b\u4ef6\u7684\u6e90\u5934\u5143\u7d20<\/h3>\n<p>\u5f53\u4e8b\u4ef6\u5192\u6ce1\u7ecf\u8fc7\u591a\u5c42\u65f6\uff0c\u5f88\u96be\u8ffd\u8e2a\u5230\u662f\u54ea\u4e2a\u5143\u7d20\u4ea7\u751f\u4e86\u8fd9\u4e00\u4e32\u7684\u4e8b\u4ef6\u3002\u53ef\u662f JavaScript \u4e2d\u5f88\u5bb9\u6613\u505a\u5230\u3002<\/p>\n<p>\u50cf\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u82e5\u6211\u4eec\u70b9\u51fb\u4e86 p \u6807\u7b7e\uff0c<code>target<\/code> \u6216 <code>event.target<\/code> \u5c06\u4f1a\u6307\u5411\u5b83\uff0c\u65e0\u8bba\u4e8b\u4ef6\u5192\u6ce1\u4e86\u591a\u5c11\u5c42\uff0c\u800c <code>event.target<\/code> \u6c38\u8fdc\u4e0d\u4f1a\u6539\u53d8\uff0c\u6307\u5411\u4e8b\u4ef6\u4ea7\u751f\u7684\u6e90\u5934\u3002<\/p>\n<h3><a id=\"%E5%A6%82%E4%BD%95%E9%98%BB%E6%AD%A2%E4%BA%8B%E4%BB%B6%E5%86%92%E6%B3%A1%EF%BC%9F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5982\u4f55\u963b\u6b62\u4e8b\u4ef6\u5192\u6ce1\uff1f<\/h3>\n<p>\u5192\u6ce1\u7684\u4e8b\u4ef6\u5c06\u4e00\u76f4\u4f20\u9012\u5230 <code>&lt;html&gt;<\/code> \u5143\u7d20\uff0c\u6709\u4e9b\u8fd8\u4f1a\u5230 <code>document<\/code>\uff0c\u5176\u4e2d\u4e00\u4e9b\u8fdb\u5165<code>window<\/code>\u5bf9\u8c61\u3002<\/p>\n<p>\u6211\u4eec\u5982\u679c\u4e0d\u60f3\u7236\u7ea7\u5143\u7d20\u7684\u4e8b\u4ef6\u53d1\u751f\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>event.stopPropagation()<\/code>\u3002<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-plain_text\">&lt;div onclick=&quot;alert('This will not alert')&quot;&gt;\n  &lt;button onclick=&quot;event.stopPropagation()&quot;&gt;Click me&lt;\/button&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n<p>\u82e5\u6211\u4eec\u70b9\u51fb\u4e86\u5e26\u6709 <code>stopPropagation()<\/code>\u7684\u6309\u94ae\uff0cdiv \u7684\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u6216 alert \u4e0d\u4f1a\u89e6\u53d1\u3002<\/p>\n<h3><a id=\"%E5%A6%82%E4%BD%95%E9%98%BB%E6%AD%A2%E5%90%8C%E4%B8%80%E4%B8%AA%E5%85%83%E7%B4%A0%E4%B8%8A%E7%9A%84%E5%A4%9A%E4%B8%AA%E4%BA%8B%E4%BB%B6%EF%BC%9F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5982\u4f55\u963b\u6b62\u540c\u4e00\u4e2a\u5143\u7d20\u4e0a\u7684\u591a\u4e2a\u4e8b\u4ef6\uff1f<\/h3>\n<p>\u6709\u65f6\u5019\u6211\u4eec\u4f1a\u4e3a\u540c\u4e00\u4e2a\u5143\u7d20\u7ed1\u5b9a\u540c\u4e00\u4e2a\u4e8b\u4ef6\u7ed1\u5b9a\u591a\u4e2a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u6709\u65f6\u5019\u671f\u671b\u963b\u6b62\u5192\u6ce1\u4e5f\u60f3\u540e\u9762\u6ce8\u518c\u7684\u540c\u7c7b\u578b\u4e8b\u4ef6\u4e5f\u88ab\u963b\u6b62\uff0c<code>event.stopImmediatePropagation()<\/code> \u5c31\u53ef\u4ee5\u505a\u5230\u3002<\/p>\n<pre><code class=\"language-plain_text\">  &lt;div onclick=&quot;alert(1)&quot;&gt;\n    &lt;div onclick=&quot;alert(2)&quot;&gt;\n      &lt;div id=&quot;target&quot;&gt;ppp&lt;\/div&gt;\n    &lt;\/div&gt;\n  &lt;\/div&gt;\n<\/code><\/pre>\n<pre><code class=\"language-plain_text\">let target = document.querySelector('#target')\n\nfunction testClick1(event){\n  alert(33)\n}\n\nfunction testClick2(event){\n  alert(44)\n  event.stopImmediatePropagation()\n  \/\/event.stopPropagation()\n}\n\n\nfunction testClick3(){\n  alert(55)\n}\n\nfunction moveHandler(){\n  alert('move')\n}\n\n\ntarget.addEventListener('click',testClick1)\n\ntarget.addEventListener('click',testClick2)\n\ntarget.addEventListener('click',testClick3)\n<\/code><\/pre>\n<p>\u4e0a\u9762\u4f1a\u4f9d\u6b21\u5f39\u51fa\uff1a<strong>33\uff0c44<\/strong>\uff0c\u82e5\u628a\u4e0a\u9762\u7684\u6ce8\u91ca\u6362\u4e00\u4e0b\u5219\u4f9d\u6b21\u5f39\u51fa\uff1a<strong>33\uff0c44\uff0c55<\/strong>\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/05\/16520166846085.jpg\" alt=\"\" \/><\/p>\n<p>\u5728\u7ebf\u6848\u4f8b\uff1a<a href=\"https:\/\/jsbin.com\/xilorahomi\/edit?html,js,output\" target=\"_blank\" rel=\"noopener\">https:\/\/jsbin.com\/xilorahomi\/edit?html,js,output<\/a><\/p>\n<p>\u53c2\u8003\u6587\u6863\uff1a<a href=\"http:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Event\/stopImmediatePropagation\" target=\"_blank\" rel=\"noopener\">http:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Event\/stopImmediatePropagation<\/a><\/p>\n<h3><a id=\"%E4%B8%BA%E4%BD%95%E9%98%BB%E6%AD%A2%E4%BA%8B%E4%BB%B6%E5%86%92%E6%B3%A1%E5%8F%AF%E8%83%BD%E4%BC%9A%E6%98%AF%E9%94%99%E8%AF%AF%E7%9A%84%EF%BC%9F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e3a\u4f55\u963b\u6b62\u4e8b\u4ef6\u5192\u6ce1\u53ef\u80fd\u4f1a\u662f\u9519\u8bef\u7684\uff1f<\/h3>\n<p>\u5982\u679c\u7528\u6237\u70b9\u51fb\u7684\u5143\u7d20\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u5e26\u6709<code>stopPropagation()<\/code> \uff0c document \u4e0a\u7684\u70b9\u51fb\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff08\u4e3a\u4e86\u8ddf\u8e2a\u3001\u5206\u6790\u6216\u8c03\u7528\u5f39\u6846\uff09\u4e5f\u4e0d\u4f1a\u88ab\u89e6\u53d1\u3002\u6240\u4ee5\u8c28\u614e\u7684\u963b\u6b62\u4e8b\u4ef6\u5192\u6ce1\u3002\u8fd9\u4e2a\u4ec5\u4ec5\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u8fd8\u6709\u8bb8\u591a\u5176\u5b83\u7684\u526f\u4f5c\u7528\u3002<\/p>\n<h3><a id=\"%E4%BA%8B%E4%BB%B6%E5%86%92%E6%B3%A1%E7%9A%84%E4%BE%8B%E5%A4%96%E6%83%85%E5%86%B5\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e8b\u4ef6\u5192\u6ce1\u7684\u4f8b\u5916\u60c5\u51b5<\/h3>\n<p>\u5e76\u4e0d\u662f\u6240\u6709\u7684\u4e8b\u4ef6\u90fd\u4f1a\u5192\u6ce1\uff0c\u4efb\u4f55\u4e0e\u7279\u5b9a\u5143\u7d20\u7ed1\u5b9a\u4e8b\u4ef6\u4e0d\u4f1a\u5192\u6ce1\uff0c\u5982\u4e0b\u4e00\u4e9b\u4e8b\u4ef6\uff1a<\/p>\n<ul>\n<li>load<\/li>\n<li>unload<\/li>\n<li>focus<\/li>\n<li>blur<\/li>\n<\/ul>\n<h2><a id=\"%E4%BA%8B%E4%BB%B6%E6%8D%95%E8%8E%B7\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e8b\u4ef6\u6355\u83b7<\/h2>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/05\/16518843756649.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/event-capturing-40o\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/event-capturing-40o<\/a><\/p>\n<p>\u4e8b\u4ef6\u6355\u83b7\u521a\u597d\u548c\u4e8b\u4ef6\u5192\u6ce1\u76f8\u53cd\uff0c\u4e8b\u4ef6\u5192\u6ce1\u4e2d\u4e8b\u4ef6\u662f\u4ece\u6700\u5185\u5c42\u5143\u7d20\u9010\u6e10\u5411\u5916\u6269\u6563\uff0c\u800c\u4e8b\u4ef6\u6355\u83b7\u5219\u662f\u4ece\u6700\u5916\u9762\u5143\u7d20\u5411\u5185\u76f4\u5230\u76ee\u6807\u5143\u7d20\u3002<\/p>\n<p>\u4e8b\u4ef6\u6355\u83b7\u5f88\u5c11\u7528\u5230\uff0c\u5f00\u542f\u4e8b\u4ef6\u6355\u83b7\u53ef\u4ee5\u7ed9 <code>addEvenListener<\/code> \u4f20\u9012\u7b2c\u4e09\u4e2a\u53c2\u6570\u3002<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-plain_text\">const myButton = document.querySelector(&quot;.btn-primary&quot;);\n\nmyButton.addEventListener(&quot;click&quot;, function() {\n    console.log(&quot;The button was clicked&quot;);\n}, { capture : true });\n<\/code><\/pre>\n<p>\u7b2c\u4e09\u4e2a\u53c2\u6570\u8bbe\u7f6e\u4e3a true \u6765\u5f00\u542f\u6355\u83b7\uff0c\u73b0\u5728\u5f53\u4e00\u4e2a\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u5b83\u4f1a\u4ece\u9876\u90e8\u4e00\u76f4\u5411\u5185\u6d41\u5230\u76ee\u6807\u5143\u7d20\uff0c\u4e4b\u540e\u4e8b\u4ef6\u518d\u5192\u6ce1\u3002<\/p>\n<pre><code class=\"language-plain_text\">&lt;div id=&quot;p&quot;&gt;\n  &lt;div id=&quot;m&quot;&gt;\n    &lt;div id=&quot;c&quot;&gt;Bubble and Capture&lt;\/div&gt;\n  &lt;\/div&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u7ed3\u6784\u53ea\u5728\u6700\u5185\u5c42\u7684\u4e8b\u4ef6\u5f00\u542f\u6355\u83b7\u4e5f\u662f\u4e0d\u884c\u7684\uff1a<\/p>\n<pre><code class=\"language-plain_text\">let p = document.querySelector('#p')\nlet m = document.querySelector('#m')\nlet c = document.querySelector('#c')\n\np.addEventListener('click',function(e){\n  alert(e.eventPhase)\n  alert('p')\n})\n\nm.addEventListener('click',function(e){\n  alert(e.eventPhase)\n  alert('m')\n})\n\nc.addEventListener('click',function(e){\n  alert(e.eventPhase)\n  alert('c')\n},true)\n<\/code><\/pre>\n<p>\u7ed3\u679c\u4e00\u6b21\u5f39\u51fa\uff1a<code>2\uff0cc\uff0c3\uff0cm\uff0c3\uff0cp<\/code> \u5176\u5b9e\u8fd8\u662f\u5192\u6ce1\u7684\u987a\u5e8f\uff0c\u53ea\u6709\u90fd\u52a0\u4e0a\u6216\u5916\u9762\u4e24\u5c42\u7684\u4e8b\u4ef6\u52a0\u4e0a\u624d\u4f1a\u6709\u6355\u83b7\u7684\u6548\u679c\uff1a<\/p>\n<pre><code class=\"language-plain_text\">let p = document.querySelector('#p')\nlet m = document.querySelector('#m')\nlet c = document.querySelector('#c')\n\np.addEventListener('click',function(e){\n  alert(e.eventPhase)\n  alert('p')\n},true)\n\nm.addEventListener('click',function(e){\n  alert(e.eventPhase)\n  alert('m')\n},true)\n\nc.addEventListener('click',function(e){\n  alert(e.eventPhase)\n  alert('c')\n})\n<\/code><\/pre>\n<p>\u5f39\u6846\u7ed3\u679c\uff1a<code>1\uff0cp\uff0c1\uff0cm\uff0c2\uff0cc<\/code>\uff0c\u82e5\u53ea\u7ed9\u4e2d\u95f4\u7684\u5f00\u542f\u6355\u83b7\u5462\uff1f<\/p>\n<pre><code class=\"language-plain_text\">let p = document.querySelector('#p')\nlet m = document.querySelector('#m')\nlet c = document.querySelector('#c')\n\np.addEventListener('click',function(e){\n  alert(e.eventPhase)\n  alert('p')\n})\n\nm.addEventListener('click',function(e){\n  alert(e.eventPhase)\n  alert('m')\n},true)\n\nc.addEventListener('click',function(e){\n  alert(e.eventPhase)\n  alert('c')\n})\n<\/code><\/pre>\n<p>\u7ed3\u679c\uff1a<code>1\uff0cm\uff0c2\uff0cc\uff0c3\uff0cp<\/code>\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/05\/16520216389621.jpg\" alt=\"16520216389621\" \/><\/p>\n<p>\u5b9e\u4f8b\uff1a<a href=\"https:\/\/jsbin.com\/wurabalaje\/edit?html,js,output%EF%BC%8C%E5%9C%A8%E6%A1%88%E4%BE%8B%E9%87%8C%E6%9D%A5%E5%9B%9E%E5%88%87%E6%8D%A2%E4%B8%80%E4%B8%8Btrue\" target=\"_blank\" rel=\"noopener\">https:\/\/jsbin.com\/wurabalaje\/edit?html,js,output\uff0c\u5728\u6848\u4f8b\u91cc\u6765\u56de\u5207\u6362\u4e00\u4e0btrue<\/a> \u518d\u5bf9\u7167\u4e0a\u56fe\u7406\u89e3\u3002<\/p>\n<p>\u7b2c\u4e09\u4e2a\u53c2\u6570\u5e76\u4e0d\u4e00\u5b9a\u8981\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u662f\u4e00\u4e2a boolean \u503c <code>true<\/code> \u4e5f\u884c\u3002<\/p>\n<pre><code class=\"language-plain_text\">myButton.addEventListener(&quot;click&quot;, function() {\n    console.log(&quot;The button was clicked&quot;);\n}, true);\n<\/code><\/pre>\n<p>\u603b\u7ed3\uff0cDOM \u4e8b\u4ef6\u6709\u4e09\u4e2a\u9636\u6bb5\uff1a<\/p>\n<ol>\n<li>\u6355\u83b7<\/li>\n<li>\u76ee\u6807\u5143\u7d20<\/li>\n<li>\u5192\u6ce1<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/05\/16520194731085.jpg\" alt=\"\" \/><\/p>\n<p>\u901a\u8fc7 <code>event.eventPhase<\/code> \u53ef\u4ee5\u786e\u5b9a\u6211\u4eec\u6240\u5904\u7684\u9636\u6bb5\u6216\u5728\u54ea\u4e2a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u4e2d\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u82e5<code>addEventListener<\/code>\u4e2d\u4e3a\u4e86\u6355\u83b7\u4f7f\u7528\u4e86 <code>true<\/code>\uff0c\u90a3<code>removeEventListener<\/code>\u65f6\u4e5f\u8981\u4f7f\u7528\u76f8\u540c\u7684\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e8b\u4ef6\u548c\u4e8b\u4ef6\u5904\u7406 \u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/events&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[249,2,20,3],"tags":[118,119,117,6],"class_list":["post-1139","post","type-post","status-publish","format-standard","hentry","category-javascript","category-all","category-frontend","category-tech","tag-bubble","tag-capture","tag-event","tag-javascript"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1139"}],"version-history":[{"count":2,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1139\/revisions"}],"predecessor-version":[{"id":1142,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1139\/revisions\/1142"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}