{"id":3003,"date":"2024-11-10T14:50:43","date_gmt":"2024-11-10T06:50:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3003\/"},"modified":"2024-11-10T14:50:43","modified_gmt":"2024-11-10T06:50:43","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-javascript-%e4%b8%ba-html-%e5%85%83%e7%b4%a0%e7%bb%91%e5%ae%9a%e5%a4%9a%e4%b8%aa-dom-%e4%ba%8b%e4%bb%b6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3003\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 JavaScript \u4e3a HTML \u5143\u7d20\u7ed1\u5b9a\u591a\u4e2a DOM \u4e8b\u4ef6\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173096375868010.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u4e3a HTML \u5143\u7d20\u7ed1\u5b9a\u591a\u4e2a DOM \u4e8b\u4ef6\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u4e3a HTML \u5143\u7d20\u7ed1\u5b9a\u591a\u4e2a DOM \u4e8b\u4ef6\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>javascript \u4e2d\u8bbe\u7f6e\u591a\u79cd dom \u4e8b\u4ef6<\/strong><\/p>\n<p>\u5728 javascript \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 addeventlistener \u65b9\u6cd5\u4e3a html \u5143\u7d20\u7ed1\u5b9a\u591a\u4e2a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u4e8b\u4ef6\u7c7b\u578b\u53c2\u6570<\/strong><\/p>\n<p>\u5982\u679c\u60a8\u60f3\u8981\u6839\u636e\u53c2\u6570\u8bbe\u7f6e\u4e0d\u540c\u7684\u4e8b\u4ef6\u7c7b\u578b\uff0c\u53ef\u4ee5\u5c06\u4e8b\u4ef6\u7c7b\u578b\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 addeventlistener \u65b9\u6cd5\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>function seteventhandler(element, eventtype, callback) {\n  element.addeventlistener(eventtype, callback);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728 seteventhandler \u51fd\u6570\u4e2d\uff0celement \u662f\u76ee\u6807\u5143\u7d20\uff0ceventtype \u662f\u8981\u7ed1\u5b9a\u7684\u4e8b\u4ef6\u7c7b\u578b\uff08\u4f8b\u5982\uff0c&#8221;click&#8221;\u3001&#8221;mouseover&#8221; \u7b49\uff09\uff0ccallback \u662f\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002<\/p>\n<p><strong>\u7ed1\u5b9a\u591a\u4e2a\u4e8b\u4ef6<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u4e00\u6b21\u6027\u4e3a\u5143\u7d20\u7ed1\u5b9a\u591a\u4e2a\u4e8b\u4ef6\uff1a<\/p>\n<pre>const events = ['click', 'mouseover', 'mouseout'];\n\nevents.foreach(function(eventtype) {\n  element.addeventlistener(eventtype, function() {\n    \/\/ \u4e8b\u4ef6\u5904\u7406\u903b\u8f91\n  });\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cevents \u6570\u7ec4\u5305\u542b\u4e86\u8981\u7ed1\u5b9a\u7684\u4e8b\u4ef6\u7c7b\u578b\uff0c\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u4e8b\u4ef6\u7c7b\u578b\u5e76\u5c06\u5176\u7ed1\u5b9a\u5230 element \u5143\u7d20\u4e0a\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u4e3a\u4e00\u4e2a <\/p>\n<p> \u5143\u7d20\u7ed1\u5b9a click \u548c mouseover \u4e8b\u4ef6\uff1a <\/p>\n<pre>const element = document.querySelector('div');\n\nelement.addEventListener('click', function() {\n  console.log('Click event triggered');\n});\n\nelement.addEventListener('mouseover', function() {\n  console.log('Mouseover event triggered');\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u60a8\u53ef\u4ee5\u540c\u65f6\u4e3a div \u5143\u7d20\u8bbe\u7f6e click \u548c mouseover \u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 JavaScript \u4e3a HTML \u5143\u7d20\u7ed1\u5b9a\u591a\u4e2a DOM \u4e8b\u4ef6\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>javascript \u4e2d\u8bbe\u7f6e\u591a\u79cd dom \u4e8b\u4ef6 \u5728 javascript \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 addeventlistener \u65b9\u6cd5\u4e3a html \u5143\u7d20\u7ed1\u5b9a\u591a\u4e2a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a \u4e8b\u4ef6\u7c7b\u578b\u53c2\u6570 \u5982\u679c\u60a8\u60f3\u8981\u6839\u636e\u53c2\u6570\u8bbe\u7f6e\u4e0d\u540c\u7684\u4e8b\u4ef6\u7c7b\u578b\uff0c\u53ef\u4ee5\u5c06\u4e8b\u4ef6\u7c7b\u578b\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 addeventlistener \u65b9\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b function seteventhandler(element, eventtype, callback) { element.addeventlistener(eventtype, callback); } \u767b\u5f55\u540e\u590d\u5236 \u5728 seteventhandler \u51fd\u6570\u4e2d\uff0celement \u662f\u76ee\u6807\u5143\u7d20\uff0ceventtype \u662f\u8981\u7ed1\u5b9a\u7684\u4e8b\u4ef6\u7c7b\u578b\uff08\u4f8b\u5982\uff0c&#8221;click&#8221;\u3001&#8221;mouseover&#8221; \u7b49\uff09\uff0ccallback \u662f\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002 \u7ed1\u5b9a\u591a\u4e2a\u4e8b\u4ef6 \u60a8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u4e00\u6b21\u6027\u4e3a\u5143\u7d20\u7ed1\u5b9a\u591a\u4e2a\u4e8b\u4ef6\uff1a const events = [&#8216;click&#8217;, &#8216;mouseover&#8217;, &#8216;mouseout&#8217;]; events.foreach(function(eventtype) { element.addeventlistener(eventtype, function() { \/\/ \u4e8b\u4ef6\u5904\u7406\u903b\u8f91 }); }); \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cevents \u6570\u7ec4\u5305\u542b\u4e86\u8981\u7ed1\u5b9a\u7684\u4e8b\u4ef6\u7c7b\u578b\uff0c\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u4e8b\u4ef6\u7c7b\u578b\u5e76\u5c06\u5176\u7ed1\u5b9a\u5230 element \u5143\u7d20\u4e0a\u3002 \u793a\u4f8b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3003","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3003","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3003"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3003\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}