2013-07-03 3 views
3

나는 .on('click','td', function(){}) 이벤트를 가지고 있습니다. 나는 그것을 .off('click','td')으로 바꾼다. 나는 지금 같은 이벤트를 다시 .on()하고 싶습니다. .on()의 조합을 시도했지만 제대로 작동하지 않았습니다. 변수에 할당하여 수행 할 수 있습니다. 어떻게 수행할지 확실하지 않습니다..on() .off() 이후의 jQuery 이벤트

+1

[이 문제를 복제 할 수 없습니다.] (http://jsfiddle.net/UcHj9/) – Ohgodwhy

+0

Do 전체 기능을 다시 정의해야합니까? 나는 전에 정의한 사건을 원한다. 변수에 할당하여 수행 할 수 있지만 어떻게 수행해야할지 확신 할 수 없습니다. – change

답변

6

변수에 이벤트 핸들러 함수를 넣어 아이디어를 얻을 희망 이것을 다음과 같이 사용하십시오.

var handler = function(e){ 
    //code here 
} 

//on 
$('#myselector').on('click', handler); 

//off 
//Try this, this will only turn 'handler' off 
$('#myselector').off('click', handler); 

//If the above doesn't work, then try this 
//This will turn off all your other click handlers for the same element, 
//if for some reason turning off a particular handler doesn't work! 
$('#myselector').off('click'); 

//on again 
$('#myselector').on('click', handler); 
4

TD를 클릭하면 다음과 같이 가정합니다. 먼저 켜기/끄기 여부를 알려주는 클래스가 있습니다. 기본적으로 TD는 td-off 클래스를 갖습니다.

<td class="td-off"> 

그런 다음 클래스를 변경하는 이벤트를 설정하십시오. 다음

.on('click','td',function(){ 

    if($(this).hasClass('td-off')){ 
    $(this).removeClass('td-off'); 
    $(this).addClass('td-on'); 
    }else{ 
    $(this).removeClass('td-on'); 
    $(this).addClass('td-off'); 
    } 

}); 

마침내 온의 이벤트를 설정하거나

$('.td-on').click(function(){ 
    //Your event on 
}); 

$('.td-off').click(function(){ 
    //Your event off 
}); 

떨어져 그것은 가장 좋은 대답하지 않을 수 있습니다하지만 난 당신이

+0

내가 왜 didnot 그것에 대해 생각하는지 모르겠다. 내가 시도해 보자, 일해야한다. – change