{"id":3230,"date":"2014-11-07T11:26:20","date_gmt":"2014-11-07T03:26:20","guid":{"rendered":"https:\/\/sdeno.com\/?p=3230"},"modified":"2016-11-19T15:31:35","modified_gmt":"2016-11-19T07:31:35","slug":"addeventlistener-%e7%9a%84%e7%94%a8%e6%b3%95%e7%a4%ba%e4%be%8b%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=3230","title":{"rendered":"addEventListener \u7684\u7528\u6cd5\u793a\u4f8b\u4ecb\u7ecd"},"content":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86addEventListener \u7684\u7528\u6cd5,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/p>\n<p>(\u8981\u6ce8\u610f\u7684\u662fdiv\u5fc5\u987b\u653e\u5230js\u524d\u9762\u624d\u884c)<\/p>\n<p>&nbsp;<\/p>\n<p>addEventListener\u4e2d\u7684\u7b2c\u4e09\u4e2a\u53c2 \u6570\u662fuseCapture, \u4e00\u4e2abool\u7c7b\u578b\u3002\u5f53\u4e3afalse\u65f6\u4e3a\u5192\u6ce1\u83b7\u53d6(\u7531\u91cc\u5411\u5916)\uff0ctrue\u4e3acapture\u65b9\u5f0f(\u7531\u5916\u5411\u91cc)\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u7ed9\u4e00\u4e2adom\u5bf9\u8c61\u7ed1\u5b9a\u540c\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u53ea\u6709\u6700\u540e\u4e00\u4e2a\u4f1a\u751f\u6548\uff0c\u6bd4\u5982\uff1a\u00a0<\/span><\/p>\n<div class=\"codetitle\" style=\"color: #000000;\"><a id=\"copybut18561\" class=\"copybut\" style=\"color: #333333;\"><\/a><span style=\"text-decoration: underline;\">\u590d\u5236\u4ee3\u7801<\/span>\u4ee3\u7801\u5982\u4e0b:<\/div>\n<div id=\"code18561\" class=\"codebody\" style=\"color: #000000;\">document.getElementById(&#8220;btn&#8221;).onclick = method1;<br \/>\ndocument.getElementById(&#8220;btn&#8221;).onclick = method2;<br \/>\ndocument.getElementById(&#8220;btn&#8221;).onclick = method3;<\/div>\n<p><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">\u90a3\u4e48\u5c06\u53ea\u6709method3\u751f\u6548\u3002\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">\u5982\u679c\u662fMozilla\u7cfb\u5217\uff0c\u7528addEventListener\u53ef\u4ee5\u8ba9\u591a\u4e2a\u4e8b\u4ef6\u6309\u987a\u5e8f\u90fd\u5b9e\u73b0\uff0c\u6bd4\u5982\uff1a\u00a0<\/span><\/p>\n<div class=\"codetitle\" style=\"color: #000000;\"><a id=\"copybut10275\" class=\"copybut\" style=\"color: #333333;\"><\/a><span style=\"text-decoration: underline;\">\u590d\u5236\u4ee3\u7801<\/span>\u4ee3\u7801\u5982\u4e0b:<\/div>\n<div id=\"code10275\" class=\"codebody\" style=\"color: #000000;\">var btn1Obj = document.getElementById(&#8220;btn1&#8221;);<br \/>\n\/\/element.addEventListener(type,listener,useCapture);<br \/>\nbtn1Obj.addEventListener(&#8220;click&#8221;,method1,false);<br \/>\nbtn1Obj.addEventListener(&#8220;click&#8221;,method2,false);<br \/>\nbtn1Obj.addEventListener(&#8220;click&#8221;,method3,false);<\/div>\n<p><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">\u6267\u884c\u987a\u5e8f\u4e3amethod1-&gt;method2-&gt;method3\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">\u5982\u679c\u662fie\u7cfb\u5217\uff0c\u7528attachEvent\u53ef\u4ee5\u8ba9\u591a\u4e2a\u4e8b\u4ef6\u6309\u987a\u5e8f\u90fd\u5b9e\u73b0\uff0c\u6bd4\u5982\uff1a\u00a0<\/span><\/p>\n<div class=\"codetitle\" style=\"color: #000000;\"><a id=\"copybut88249\" class=\"copybut\" style=\"color: #333333;\"><\/a><span style=\"text-decoration: underline;\">\u590d\u5236\u4ee3\u7801<\/span>\u4ee3\u7801\u5982\u4e0b:<\/div>\n<div id=\"code88249\" class=\"codebody\" style=\"color: #000000;\">var btn1Obj = document.getElementById(&#8220;btn1&#8221;);<br \/>\n\/\/object.attachEvent(event,function);<br \/>\nbtn1Obj.attachEvent(&#8220;onclick&#8221;,method1);<br \/>\nbtn1Obj.attachEvent(&#8220;onclick&#8221;,method2);<br \/>\nbtn1Obj.attachEvent(&#8220;onclick&#8221;,method3);<\/div>\n<p><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">\u6267\u884c\u987a\u5e8f\u4e3amethod3-&gt;method2-&gt;method1\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">=======================================================\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">Mozilla\u4e2d\uff1a\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">addEventListener\u7684\u4f7f\u7528\u65b9\u5f0f\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">target.addEventListener(type,listener,useCapture);\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">target\uff1a \u6587\u6863\u8282\u70b9\u3001document\u3001window \u6216 XMLHttpRequest\u3002\u00a0<\/span><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">type\uff1a \u5b57\u7b26\u4e32\uff0c\u4e8b\u4ef6\u540d\u79f0\uff0c\u4e0d\u542b\u201con\u201d\uff0c\u6bd4\u5982\u201cclick\u201d\u3001\u201cmouseover\u201d\u3001\u201ckeydown\u201d\u7b49\u3002\u00a0<\/span><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">listener \uff1a\u5b9e\u73b0\u4e86 EventListener \u63a5\u53e3\u6216\u8005\u662f JavaScript \u4e2d\u7684\u51fd\u6570\u3002\u00a0<\/span><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">useCapture \uff1a\u662f\u5426\u4f7f\u7528\u6355\u6349\uff0c\u4e00\u822c\u7528 false \u3002\u4f8b\u5982\uff1adocument.getElementById(&#8220;testText&#8221;).addEventListener(&#8220;keydown&#8221;, function (event) { alert(event.keyCode); }, false);\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">IE\u4e2d\uff1a\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">target.attachEvent(type, listener);\u00a0<\/span><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">target\uff1a \u6587\u6863\u8282\u70b9\u3001document\u3001window \u6216 XMLHttpRequest\u3002\u00a0<\/span><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">type\uff1a \u5b57\u7b26\u4e32\uff0c\u4e8b\u4ef6\u540d\u79f0\uff0c\u542b\u201con\u201d\uff0c\u6bd4\u5982\u201conclick\u201d\u3001\u201conmouseover\u201d\u3001\u201conkeydown\u201d\u7b49\u3002\u00a0<\/span><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">listener \uff1a\u5b9e\u73b0\u4e86 EventListener \u63a5\u53e3\u6216\u8005\u662f JavaScript \u4e2d\u7684\u51fd\u6570\u3002 \u4f8b\u5982\uff1adocument.getElementById(&#8220;txt&#8221;).attachEvent(&#8220;onclick&#8221;,function(event){alert(event.keyCode);});\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">W3C \u53ca IE \u540c\u65f6\u652f\u6301\u79fb\u9664\u6307\u5b9a\u7684\u4e8b\u4ef6, \u7528\u9014\u662f\u79fb\u9664\u8bbe\u5b9a\u7684\u4e8b\u4ef6, \u683c\u5f0f\u5206\u522b\u5982\u4e0b:\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">removeEventListener(event,function,capture\/bubble); \u00a0 \u00a0 \/\/<span style=\"color: #ff0000;\">\u8fd9\u91cc\u8981\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684function\u5fc5\u987b\u662f\u540c\u4e00\u4e2athis\u4f5c\u7528\u57df\uff0c\u5426\u5219\u4e0d\u80fd\u79fb\u9664\u4e4b\u524d\u7ed1\u5b9a\u7684\u4e8b\u4ef6<\/span><\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">Windows IE\u7684\u683c\u5f0f\u5982\u4e0b:\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">detachEvent(event,function);\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">DOM2 \u7684\u8fdb\u5316:\u00a0<\/span><\/p>\n<table class=\"jbborder\" style=\"color: #000000;\">\n<tbody>\n<tr>\n<td><strong>DOM 0 Event<\/strong><\/td>\n<td><strong>DOM 2 Event<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">onblur()<\/td>\n<td valign=\"top\">blur<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">onfocus()<\/td>\n<td valign=\"top\">focus<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">onchange()<\/td>\n<td valign=\"top\">change<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">onmouseover()<\/td>\n<td valign=\"top\">mouseover<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">onmouseout()<\/td>\n<td valign=\"top\">mouseout<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">onmousemove()<\/td>\n<td valign=\"top\">mousemove<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">onmousedown()<\/td>\n<td valign=\"top\">mousedown<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">onmouseup()<\/td>\n<td valign=\"top\">mouseup<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">onclick()<\/td>\n<td valign=\"top\">click<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">ondblclick()<\/td>\n<td valign=\"top\">dblclick<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">onkeydown()<\/td>\n<td valign=\"top\">keydown<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">onkeyup()<\/td>\n<td valign=\"top\">keyup<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">onkeypress()<\/td>\n<td valign=\"top\">keypress<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">onsubmit()<\/td>\n<td valign=\"top\">submit<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">onload()<\/td>\n<td valign=\"top\">load<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">onunload()<\/td>\n<td valign=\"top\">unload<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">\u65b0\u7684DOM2 \u7528\u6cd5\u53ef\u4ee5addEventListener()\u8fd9\u4e2a\u51fd\u6570\u6765\u89c2\u5bdf\u5230:\u00a0<\/span><\/p>\n<div class=\"codetitle\" style=\"color: #000000;\"><a id=\"copybut27770\" class=\"copybut\" style=\"color: #333333;\"><\/a><span style=\"text-decoration: underline;\">\u590d\u5236\u4ee3\u7801<\/span>\u4ee3\u7801\u5982\u4e0b:<\/div>\n<div id=\"code27770\" class=\"codebody\" style=\"color: #000000;\">addEventListener(event,function,capture\/bubble);<\/div>\n<p><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">\u53c2\u6570event\u5982\u4e0a\u8868\u6240\u793a, function\u662f\u8981\u6267\u884c\u7684\u51fd\u6570, capture\u4e0ebubble\u5206\u522b\u662fW3C\u5236\u5b9a\u5f97\u4e24\u79cd\u65f6\u95f4\u6a21\u5f0f,\u7b80\u5355\u6765\u8bf4capture\u5c31\u662f\u4ecedocument\u7684\u5f00\u59cb\u8bfb\u5230\u6700\u540e\u4e00\u884c, \u518d\u6267\u884c\u4e8b\u4ef6, \u800cbubble\u5219\u662f\u5148\u5bfb\u627e\u6307\u5b9a\u7684\u4f4d\u7f6e\u518d\u6267\u884c\u4e8b\u4ef6.\u00a0<\/span><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">capture\/bubble\u7684\u53c2\u6570\u662f\u5e03\u5c14\u503c, True\u8868\u793a\u7528capture, False\u5219\u662fbubble.Windows Internet Explorer\u4e5f\u6709\u5236\u5b9a\u4e00\u79cdEventHandler, \u662f attachEvent(), \u683c\u5f0f\u5982\u4e0b:\u00a0<\/span><\/p>\n<div class=\"codetitle\" style=\"color: #000000;\"><a id=\"copybut9207\" class=\"copybut\" style=\"color: #333333;\"><\/a><span style=\"text-decoration: underline;\">\u590d\u5236\u4ee3\u7801<\/span>\u4ee3\u7801\u5982\u4e0b:<\/div>\n<div id=\"code9207\" class=\"codebody\" style=\"color: #000000;\">window.attachEvent(\u201dsubmit\u201d,myFunction());<\/div>\n<p><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">\u6bd4\u8f83\u7279\u522b\u7684\u662fattachEvent\u4e0d\u9700\u8981\u6307\u5b9acapture\/bubble\u7684\u53c2\u6570, \u56e0\u4e3a\u5728windows IE\u73af\u5883\u4e0b\u90fd\u662f\u4f7f\u7528Bubble\u7684\u6a21\u5f0f.<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">\u5982\u4f55\u5224\u65ad\u662f\u5426\u652f\u6301\u54ea\u79cd\u76d1\u542c\u5462\uff1f\u5982\uff1a\u00a0<\/span><\/p>\n<div class=\"codetitle\" style=\"color: #000000;\"><a id=\"copybut91454\" class=\"copybut\" style=\"color: #333333;\"><\/a><span style=\"text-decoration: underline;\">\u590d\u5236\u4ee3\u7801<\/span>\u4ee3\u7801\u5982\u4e0b:<\/div>\n<div id=\"code91454\" class=\"codebody\" style=\"color: #000000;\">if (typeof window.addEventListener != \u201cundefined\u201d) {<br \/>\nwindow.addEventListener(\u201dload\u201d,rollover,false);<br \/>\n} else {<br \/>\nwindow.attachEvent(\u201donload\u201d,rollover)<br \/>\n}<\/div>\n<p><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">\u4e0a\u8ff0\u7684 typeof window.addEventListener != \u201cundefined\u201d \u7a0b\u5e8f\u4ee3\u7801\u53ef\u4ee5\u5224\u65ad\u4f7f\u7528\u8005\u7684\u6d4f\u89c8\u5668\u662f\u5426\u652f\u6301AddEventListener\u8fd9\u4e2a\u4e8b\u4ef6\u6a21\u578b, \u5982\u679c\u4e0d\u652f\u6301\u5c31\u4f7f\u7528attachEvent.\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">W3C \u53ca IE \u540c\u65f6\u652f\u6301\u79fb\u9664\u6307\u5b9a\u7684\u4e8b\u4ef6, \u7528\u9014\u662f\u79fb\u9664\u8bbe\u5b9a\u7684\u4e8b\u4ef6, \u683c\u5f0f\u5206\u522b\u5982\u4e0b:\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">W3C\u683c\u5f0f:\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">removeEventListener(event,function,capture\/bubble);\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">Windows IE\u7684\u683c\u5f0f\u5982\u4e0b:\u00a0<\/span><br style=\"color: #000000;\" \/><br style=\"color: #000000;\" \/><span style=\"color: #000000;\">detachEvent(event,function);<\/span><\/p>\n<p><a href=\"http:\/\/www.jb51.net\/article\/49858.htm\" target=\"_blank\">http:\/\/www.jb51.net\/article\/49858.htm<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86addEventListener \u7684\u7528\u6cd5,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b (\u8981\u6ce8\u610f\u7684\u662fdiv\u5fc5\u987b\u653e\u5230j [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,11],"tags":[],"class_list":["post-3230","post","type-post","status-publish","format-standard","hentry","category-javascript","category-11"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/3230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3230"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/3230\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}