개발 도구에 해당 기능이 있을 거라 테스트해봤습니다. 전제: 단순한 작은 테스트일 뿐이며, addEventListener는 표준 바인딩 기능으로 IE에서는 이와 호환되지 않습니다(해당 IE는 attachmentEvent입니다). )이므로 이번에는 테스트를 위해 먼저 IE를 포기하고 Firefox 5.0.1, Chrome 14.0, Opera 11.50을 사용하세요. 테스트 페이지: 코드 복사 코드는 다음과 같습니다. Test< /title> <br>.test { <br>배경색: #FFF; <br>테두리: 1px 단색 회색; <br>너비: 100px; <br>여백: 10px 0; <br>float: 왼쪽 <br>} <br> TEST1 TEST2 TEST3 <br>(function(){ <br>var $ = function(o){//간단한 도구 <br>return document.getElementById(o); <br>} <br>//Test2의 경우: <br>$('test2').onclick = function(){console.log('test2 : click!');} <br>$('test2') .onmouseover = function(){console.log( 'test2 : mouseover!');} <br>//Test3의 경우: <br>$('test3').addEventListener('click', function(){ <br>console.log('test3 : click!' ); <br>}, false) <br>$('test3').addEventListener('mouseover', function(){ <br>console.log('test3 : mouseover!'); <br>})(); <br></body> 🎜> <br> <p>页面效果如下截图:</p> <p> <img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233144777.png" class="lazy" border="0" alt="onclick && addEventListener_javascript 기술에 대한 이벤트 바인딩에 대한 소규모 테스트" ></p> <p>测试代码中采用了三种最常见的事件绑定方法</p> <p>FIREFOX:</p> <p>1,Firefox 的 Firebug 中选中<strong> test1 </strong>元素:</p> <p><img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028556.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0"> </p> <p>右侧 DOM 标签中选择显示所有属性:</p> <p><img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028301.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0"> </p> <p>在其中找到了 attributes 数组,如下:</p> <p><img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028825.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0"> </p> <p>这是因为 test1 元素的两个事件句柄直接写到了元素中,再往下的列表中找不到更多相关的信息,此种绑定模式下只能在 firebug 的 DOM 标签的 attributes 中找到事件句柄。</p> <p>2,选中<strong> test2</strong> 元素:</p> <p><img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028602.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0"> </p> <p>右侧 DOM 标签:</p> <p><img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028132.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0"> </p> <p></p> <p>test2 采用在 javascript 中绑定事件句柄的方式,被作为“用户自定义属性”显示在了 DOM 标签中,右击鼠标可以查看相关信息:</p> <p><img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028317.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0"> </p> <p>3,选中 <strong>test3 </strong>元素:</p> <p><img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028974.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0"> </p> <p>这次在 DOM 标签中没有找到任何相关此元素绑定事件的信息。。。</p> <p></p> <p>总而言之,firebug 中<strong>在标准绑定事件函数下</strong>(addEventListener),并不好检测到某个元素已绑定的事件函数,(不要说尝试打印一下那个元素的 onclick 属性,addEventListener 绑定和 onclick 绑定毫不相干,此种情况下你会得到 nudefined);如果是<strong>在前两种事件绑定下</strong>,可以得到一些简单信息。</p> <p>-----</p> <p>OPERA:</p> <p>1,在 Opera 的 Dragonfly 中选中 <strong>test1 :</strong></p> <p><img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028363.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0"> </p> <p>在右方的属性列表中可以找到相关的信息:</p> <p><img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028301.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0"> </p> <p>点击加号同样可以查看绑定的函数信息:</p> <p><img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028558.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0"> </p> <p>想查看某个函数体的话,有一种简单的方法,在控制台中直接输入:</p> <p><img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028490.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0"> </p> <p>直接就打印出函数体,更加的方便!</p> <p>2,选择 <strong>test2 </strong>和<strong> test1</strong> 几乎相同,就不在赘述了;</p> <p>3,选择 <strong>test3:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028905.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0"> </p> <p>오른쪽 속성 태그에는 아무것도 찾을 수 없습니다. onclick 및 onmouseover 값은 모두 null입니다. </p> <p>요컨대 Opera Dragonfly와 Firefox Firebug는 비슷한 성능을 발휘하며 표준 바인딩 기능에서 어떤 이벤트가 요소에 바인딩되는지는 명확하지 않습니다. </p> <p>------</p> <p>크롬:</p> <p>Chrome에서 훨씬 더 명확해졌습니다. </p> <p>1, 디버깅 도구에서 <strong>test1(또는 test2, 이 둘은 유사함)을 선택합니다. </strong></p> <p><img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028982.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0"> </p> <p>오른쪽 정보 부분을 살펴보세요. </p> <p><img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028243.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0"> </p> Chrome의 <p><strong>이벤트 리스너</strong>는 현재 선택된 요소에 바인딩된 청취 기능을 직접 나열하는 좋은 도구입니다. 작은 검은색 삼각형을 클릭하면 바인딩된 기능에 대한 정보를 볼 수 있습니다. </p> <p><img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028969.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0"> </p> <p>click에는 두 가지 항목이 있습니다. 첫 번째 항목은 우리가 바인딩한 onclick 함수 정보이고, 두 번째 항목인 document는 볼 필요가 없습니다(Chrome 자체 처리). </p> <p>isAttribute: true: <strong>이 onclick 함수 핸들이 요소 속성으로 처리됨을 나타냅니다(test1이든 test2이든 onclick = function(){…}을 사용하기 때문입니다).</strong> </p> <p>lineNumber: 18: <strong>은 바인딩된 함수의 위치를 나타냅니다. </strong></p>useCapture: false :<p> 이벤트 캡처가 사용되지 않음을 나타냅니다. 다른 의미는 매우 강력하므로 언급하지 않겠습니다. <strong> </strong>마찬가지로 </p>속성<p>의 첫 번째 HTMLDivElement 목록에서 test1과 test2 모두 다음을 볼 수 있습니다. <strong> </strong></p> <p> <img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028338.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0">2, </p>test3<p> 선택: <strong> </strong></p> <p> <img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028218.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0"></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p>속성에서는 onclick과 onmouseover가 모두 null이 되지만 이벤트 리스너는 여전히 다음과 같습니다. </p> <p></p> <p> <img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028340.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0">하지만 div#test3에 주목하세요:</p> <p></p> <p> <img src="/static/imghwm/default1.png" data-src="http://files.jb51.net/upload/201107/20110731233028889.png" class="lazy" title="image" style="max-width:90%" style="max-width:90%" alt="image" style="max-width:90%" border="0">onclick 속성을 사용하지 않고 addEventListener를 사용하므로 isAttribute가 false가 됩니다. </p> <p></p> <p></p> <p>먼저 이것을 기억하세요! 글이 좀 지저분하네요~</p> <p></p> </div>