2011-12-28 3 views
0

이벤트 유형에 관계없이 이벤트 핸들러를 요소에 바인딩하는 좋은 방법이 있습니까? 당신은 bind 방법의 기본 동작을 변경할 수 있습니다 당신은 여기 이벤트의 목록을 찾을 수 있습니다모든 이벤트 유형에 대한 핸들러 연결

$('#elem').bind(function(e){ 

    //do stuff for any event originating from this element 
    e.stopPropagation(); 
}); 
+2

이 질문에 비슷한 대답을 : http://stackoverflow.com/questions/5848598/how-all-events-of- dom-element-can-be-bind –

+0

감사합니다. 어떻게 든이 질문을 게시했을 때 '비슷한 질문'섹션에 나타나지 않았습니다. :) 어쨌든, 내 질문에 '깔끔한'나는 아직도 이것을 할 다른 방법이 있는지 알고 싶다. –

답변

3

: 예를 들어

, 내가 뭔가를해야합니다. 우리가 하나의 인자를 주면 모든 사건에 이것을 덧붙입니다. 이것을보세요.

$.fn.init.prototype.bind = (function(old) { 

    return function() { 
     if(arguments.length == 1 && typeof arguments[0] === "function") { 
      // if we'll put to bind method one argument which is function 

      // list of whole of events - you set themselves as needed 
      return this.bind("click keydown keyup keypress mouseover mouseenter mouseout mouseleave mousedown mouseup mousemove change blur focus scroll resize load unload beforeunload", arguments[0]); 

     } else { 
      // trigger original bind method 
      return old.apply(this, arguments) 
     } ; 
    } 

})($.fn.init.prototype.bind); 

지금 :

$("body").bind(function() { console.log(this) }); 

휙 :

+0

쿨 :) 이것을 테스트 해 보겠습니다. –

+0

당신은 플러그인 개발자들에게 락 스타처럼 보입니다. :) –

0
var eventsList = "blur, focus, load, resize, 
       scroll, unload, beforeunload, click, dblclick, 
       mousedown, mouseup, mousemove, mouseover, mouseout, 
       mouseenter, mouseleave, change, select, submit, keydown, 
       keypress, keyup, error"; 

(/* selector of user choice */).bind(eventsList, function(){ 

//All operations here.. 
}); 

... JQuery Events