0

나는이 작은 스크립트가 있습니다Javascript Mootools 클릭 이벤트와 그의 발신자?

var moolang = new Class({ 
    initialize: function(element) { 

     this.el = $(element); 
     this.el.addEvent('click', this.popup); 

    }, 

    popup: function() 
    { 
     //this.id = the id of the element. 
    } 
}); 

을 그리고 난 "이"팝업 기능을 알고 싶습니다. 하지만 경고 (this.el.id)와 같은 것을 시도해도 이것이 없다고합니다. :/

어떤 클래스가 이벤트를 추가하는지 알 수있는 방법이 있습니까?

답변

5

호출 수신자가 적절한 컨텍스트를 가지도록 접속 이벤트를 변경하십시오. 그렇지 않으면 이벤트 리스너의 컨텍스트가 대상 요소가됩니다.

// Change this line 
this.el.addEvent('click', this.popup); 
//To this 
this.el.addEvent('click', this.popup.bind(this)); // popup->this == this 

jsfiddle here 은 Mootools의 documentation를 참조하십시오. 함수의 컨텍스트 바인딩.