{"id":2110,"date":"2025-02-27T23:49:58","date_gmt":"2025-02-27T15:49:58","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2110"},"modified":"2025-02-27T23:49:58","modified_gmt":"2025-02-27T15:49:58","slug":"javascript-html-dom-eventlistener","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/27\/javascript-html-dom-eventlistener\/","title":{"rendered":"JavaScript HTML DOM EventListener"},"content":{"rendered":"\n<p><code>EventListener<\/code> \u662f JavaScript \u4e2d\u7528\u4e8e\u4e3a DOM \u5143\u7d20\u7ed1\u5b9a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u4e8b\u4ef6\u53d1\u751f\u65f6\u8c03\u7528\u6307\u5b9a\u7684\u51fd\u6570\uff0c\u5e76\u4e14\u80fd\u591f\u4e3a\u540c\u4e00\u4e2a\u4e8b\u4ef6\u7ed1\u5b9a\u591a\u4e2a\u5904\u7406\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. <code>addEventListener()<\/code> \u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<p><code>addEventListener()<\/code> \u65b9\u6cd5\u7528\u4e8e\u5c06\u4e8b\u4ef6\u76d1\u542c\u5668\u6dfb\u52a0\u5230\u6307\u5b9a\u7684\u5143\u7d20\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>element.addEventListener(event, function, useCapture);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>event<\/code>\uff1a\u6307\u5b9a\u8981\u76d1\u542c\u7684\u4e8b\u4ef6\u7c7b\u578b\uff0c\u5982 <code>\"click\"<\/code>\u3001<code>\"mouseover\"<\/code> \u7b49\u3002<\/li>\n\n\n\n<li><code>function<\/code>\uff1a\u5f53\u4e8b\u4ef6\u89e6\u53d1\u65f6\u8c03\u7528\u7684\u56de\u8c03\u51fd\u6570\u3002<\/li>\n\n\n\n<li><code>useCapture<\/code>\uff08\u53ef\u9009\uff09\uff1a\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u8868\u793a\u4e8b\u4ef6\u662f\u5426\u5e94\u8be5\u5728\u6355\u83b7\u9636\u6bb5\u89e6\u53d1\u3002\u9ed8\u8ba4\u4e3a <code>false<\/code>\uff0c\u5373\u5728\u4e8b\u4ef6\u5192\u6ce1\u9636\u6bb5\u89e6\u53d1\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;button id=\"btn\"&gt;Click Me!&lt;\/button&gt;\n\n&lt;script&gt;\n  const btn = document.getElementById('btn');\n\n  \/\/ \u4e3a\u6309\u94ae\u7ed1\u5b9a\u70b9\u51fb\u4e8b\u4ef6\n  btn.addEventListener('click', function() {\n    alert('Button clicked!');\n  });\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u6309\u94ae\u65f6\uff0c\u4e8b\u4ef6\u76d1\u542c\u5668\u4f1a\u89e6\u53d1\u5e76\u6267\u884c\u7ed1\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u4e8b\u4ef6\u6355\u83b7\u4e0e\u4e8b\u4ef6\u5192\u6ce1<\/strong><\/h3>\n\n\n\n<p>\u4e8b\u4ef6\u76d1\u542c\u5668\u53ef\u4ee5\u901a\u8fc7 <code>useCapture<\/code> \u53c2\u6570\u63a7\u5236\u4e8b\u4ef6\u4f20\u64ad\u7684\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e8b\u4ef6\u5192\u6ce1<\/strong>\uff08\u9ed8\u8ba4\u884c\u4e3a\uff09\uff1a\u4e8b\u4ef6\u4ece\u76ee\u6807\u5143\u7d20\u5f00\u59cb\uff0c\u9010\u5c42\u5411\u4e0a\u5192\u6ce1\u81f3 <code>document<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u4e8b\u4ef6\u6355\u83b7<\/strong>\uff1a\u4e8b\u4ef6\u4ece <code>document<\/code> \u6216\u5176\u4ed6\u7236\u5143\u7d20\u5f00\u59cb\uff0c\u9010\u5c42\u5411\u4e0b\u4f20\u64ad\uff0c\u76f4\u5230\u76ee\u6807\u5143\u7d20\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4e8b\u4ef6\u6355\u83b7\u4e0e\u5192\u6ce1<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div id=\"parent\"&gt;\n  &lt;button id=\"child\"&gt;Click Me!&lt;\/button&gt;\n&lt;\/div&gt;\n\n&lt;script&gt;\n  const parent = document.getElementById('parent');\n  const child = document.getElementById('child');\n\n  \/\/ \u6355\u83b7\u9636\u6bb5\uff08\u5148\u89e6\u53d1\uff09\n  parent.addEventListener('click', function() {\n    alert('Parent clicked during capture!');\n  }, true);\n\n  \/\/ \u5192\u6ce1\u9636\u6bb5\uff08\u540e\u89e6\u53d1\uff09\n  child.addEventListener('click', function() {\n    alert('Child clicked!');\n  });\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7531\u4e8e <code>parent<\/code> \u7684\u4e8b\u4ef6\u76d1\u542c\u5668\u5728\u6355\u83b7\u9636\u6bb5\uff08\u901a\u8fc7\u4f20\u5165 <code>true<\/code>\uff09\uff0c\u6240\u4ee5\u5b83\u4f1a\u5148\u89e6\u53d1\uff0c\u800c <code>child<\/code> \u7684\u4e8b\u4ef6\u76d1\u542c\u5668\u5728\u5192\u6ce1\u9636\u6bb5\uff08\u9ed8\u8ba4\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u79fb\u9664\u4e8b\u4ef6\u76d1\u542c\u5668<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u4e0d\u518d\u9700\u8981\u67d0\u4e2a\u4e8b\u4ef6\u76d1\u542c\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>removeEventListener()<\/code> \u65b9\u6cd5\u79fb\u9664\u5b83\u3002\u8981\u79fb\u9664\u4e8b\u4ef6\u76d1\u542c\u5668\uff0c\u5fc5\u987b\u4f7f\u7528\u4e0e\u6dfb\u52a0\u65f6\u76f8\u540c\u7684\u4e8b\u4ef6\u7c7b\u578b\u3001\u56de\u8c03\u51fd\u6570\u548c <code>useCapture<\/code> \u503c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u8bed\u6cd5\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>element.removeEventListener(event, function, useCapture);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;button id=\"btn\"&gt;Click Me!&lt;\/button&gt;\n\n&lt;script&gt;\n  const btn = document.getElementById('btn');\n\n  function handleClick() {\n    alert('Button clicked!');\n  }\n\n  \/\/ \u7ed1\u5b9a\u4e8b\u4ef6\n  btn.addEventListener('click', handleClick);\n\n  \/\/ \u79fb\u9664\u4e8b\u4ef6\n  btn.removeEventListener('click', handleClick);\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4e8b\u4ef6\u76d1\u542c\u5668\u88ab\u6210\u529f\u79fb\u9664\uff0c\u56e0\u6b64\u6309\u94ae\u70b9\u51fb\u65f6\u4e0d\u4f1a\u89e6\u53d1 <code>handleClick<\/code> \u51fd\u6570\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. <code>EventListener<\/code> \u7684\u4f18\u52bf<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u591a\u4e2a\u4e8b\u4ef6\u5904\u7406\u5668<\/strong>\uff1a\u4f60\u53ef\u4ee5\u4e3a\u540c\u4e00\u4e8b\u4ef6\u7ed1\u5b9a\u591a\u4e2a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u8986\u76d6\u6216\u5220\u9664\u5176\u4ed6\u5904\u7406\u7a0b\u5e8f\u3002<\/li>\n\n\n\n<li><strong>\u4e8b\u4ef6\u6355\u83b7\u4e0e\u5192\u6ce1<\/strong>\uff1a\u4f60\u53ef\u4ee5\u7cbe\u786e\u63a7\u5236\u4e8b\u4ef6\u7684\u4f20\u64ad\u65b9\u5f0f\uff08\u6355\u83b7\u6216\u5192\u6ce1\uff09\u3002<\/li>\n\n\n\n<li><strong>\u652f\u6301\u79fb\u9664<\/strong>\uff1a\u4f7f\u7528 <code>removeEventListener()<\/code> \u53ef\u4ee5\u6e05\u9664\u4e0d\u518d\u9700\u8981\u7684\u4e8b\u4ef6\u76d1\u542c\u5668\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u4e8b\u4ef6\u5bf9\u8c61<\/strong><\/h3>\n\n\n\n<p>\u6bcf\u4e2a\u4e8b\u4ef6\u90fd\u4f1a\u4f20\u9012\u4e00\u4e2a\u4e8b\u4ef6\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5305\u542b\u5173\u4e8e\u4e8b\u4ef6\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u4f8b\u5982\u4e8b\u4ef6\u7c7b\u578b\u3001\u89e6\u53d1\u4e8b\u4ef6\u7684\u5143\u7d20\u7b49\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5e38\u89c1\u7684\u4e8b\u4ef6\u5bf9\u8c61\u5c5e\u6027<\/strong>\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>target<\/strong>\uff1a\u89e6\u53d1\u4e8b\u4ef6\u7684\u5143\u7d20\u3002<\/li>\n\n\n\n<li><strong>type<\/strong>\uff1a\u4e8b\u4ef6\u7c7b\u578b\uff08\u5982 <code>'click'<\/code>\u3001<code>'keydown'<\/code> \u7b49\uff09\u3002<\/li>\n\n\n\n<li><strong>clientX, clientY<\/strong>\uff1a\u9f20\u6807\u70b9\u51fb\u4f4d\u7f6e\u7684 X \u548c Y \u5750\u6807\uff08\u76f8\u5bf9\u4e8e\u89c6\u53e3\uff09\u3002<\/li>\n\n\n\n<li><strong>keyCode<\/strong>\uff1a\u952e\u76d8\u4e8b\u4ef6\u4e2d\u7684\u6309\u952e\u4ee3\u7801\u3002<\/li>\n\n\n\n<li><strong>preventDefault()<\/strong>\uff1a\u963b\u6b62\u4e8b\u4ef6\u7684\u9ed8\u8ba4\u884c\u4e3a\u3002<\/li>\n\n\n\n<li><strong>stopPropagation()<\/strong>\uff1a\u963b\u6b62\u4e8b\u4ef6\u8fdb\u4e00\u6b65\u4f20\u64ad\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;button id=\"btn\"&gt;Click Me!&lt;\/button&gt;\n\n&lt;script&gt;\n  const btn = document.getElementById('btn');\n\n  btn.addEventListener('click', function(event) {\n    alert('Event type: ' + event.type);\n    alert('Button clicked at: ' + event.clientX + ', ' + event.clientY);\n  });\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. \u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>addEventListener()<\/code><\/strong>\uff1a\u4e3a\u5143\u7d20\u7ed1\u5b9a\u4e8b\u4ef6\uff0c\u652f\u6301\u4e8b\u4ef6\u5192\u6ce1\u548c\u6355\u83b7\u3002<\/li>\n\n\n\n<li><strong>\u4e8b\u4ef6\u6355\u83b7\u4e0e\u5192\u6ce1<\/strong>\uff1a\u4e8b\u4ef6\u7684\u4f20\u64ad\u987a\u5e8f\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>useCapture<\/code> \u53c2\u6570\u63a7\u5236\u3002<\/li>\n\n\n\n<li><strong>\u79fb\u9664\u4e8b\u4ef6<\/strong>\uff1a\u4f7f\u7528 <code>removeEventListener()<\/code> \u79fb\u9664\u4e8b\u4ef6\u76d1\u542c\u5668\u3002<\/li>\n\n\n\n<li><strong>\u4e8b\u4ef6\u5bf9\u8c61<\/strong>\uff1a\u6bcf\u4e2a\u4e8b\u4ef6\u90fd\u5305\u542b\u4e00\u4e2a\u4e8b\u4ef6\u5bf9\u8c61\uff0c\u63d0\u4f9b\u6709\u5173\u4e8b\u4ef6\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<p><code>EventListener<\/code> \u65b9\u6cd5\u662f\u5904\u7406\u4e8b\u4ef6\u7684\u63a8\u8350\u65b9\u5f0f\uff0c\u5b83\u6bd4\u4f20\u7edf\u7684\u4e8b\u4ef6\u5c5e\u6027\uff08\u5982 <code>onclick<\/code>\uff09\u66f4\u7075\u6d3b\u548c\u5f3a\u5927\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u66f4\u597d\u5730\u7ba1\u7406\u548c\u63a7\u5236\u4e8b\u4ef6\u3002<\/p>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>EventListener \u662f JavaScript \u4e2d\u7528\u4e8e\u4e3a DOM \u5143\u7d20\u7ed1\u5b9a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u5b83\u5141\u8bb8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2110","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2110"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2110\/revisions"}],"predecessor-version":[{"id":2111,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2110\/revisions\/2111"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}