2012-01-15 2 views
0

자바 스크립트로 페이지에 정의 된 모든 이벤트 처리기를 취소하고 싶습니다. 즉, 이전에 페이지가 여러 DOM 수준에서 많은 이벤트 리스너를 설정 한 경우이 메소드는 단순히 모든 항목을 삭제합니다. Javascript에서이 작업을 수행 할 수있는 방법이 있습니까?모든 이벤트 처리기 지우기

답변

2

동적 페이지의 본문 내에서 요소에 연결된 모든 이벤트를 지우기 위해, 당신이 할 수 있습니다

document.body.innerHTML = document.body.innerHTML; 

을 window 객체에 부착 된 이벤트 지우기를 들어, 당신은 할 수 있습니다 :

window.onscroll = function() {}; 

등등 ..

+0

내 테스트'document.onmousedown' 이벤트를 지우지 않았습니다. – wecsam

+0

문서 개체에 첨부 된 이벤트를 지우기위한 바로 가기가 없습니다. 창 개체와 마찬가지로'document.onmousedown = function() {}'과 같이 하나씩 지울 필요가 있습니다. – techfoobar

+1

이것이 작동하는지 잘 모르겠습니다. 'innerHTML' 자체에 일부 이벤트를 일부 요소에 추가 한 '

1

아니요 무엇이 무엇이 바인딩되어 있는지 보려면 기본 이벤트 목록이 없습니다. JQuery는 더 나은 이벤트 관리를 원하는 경우 자체적으로 사용할 수 있습니다.

여기가 JQuery와에 끝낼 방법은 다음과 같습니다

(function($) { 
    $.eventReport = function(selector, root) { 
     var s = []; 
     $(selector || '*', root).andSelf().each(function() { 
      var e = $.data(this, 'events'); 
      if(!e) return; 
      s.push(this.tagName); 
      if(this.id) s.push('#', this.id); 
      if(this.className) s.push('.', this.className); 
      for(var p in e) s.push('\n', p); 
      s.push('\n\n'); 
     }); 
     return s.join(''); 
    } 
    $.fn.eventReport = function(selector) { 
     return $.eventReport(selector, this); 
    } 
})(jQuery); 

당신은 당신의 요구에 맞게 다양한 방법을 호출 할 수 있습니다 거기에서

// all events 
alert($.eventReport()); 

// just events on inputs 
alert($.eventReport('input')); 

// just events assigned to this element 
alert($.eventReport('#myelement')); 

// events assigned to inputs in this element 
alert($.eventReport('input', '#myelement')); 
alert($('#myelement').eventReport('input')); // same result 

// just events assigned to this element's children 
alert($('#myelement').eventReport()); 
alert($.eventReport('*', '#myelement'); // same result 

, 바인딩 해제는 간단하다.