jquery,添加和删除事件处理程序[duplicate]

时间:2021-11-05 00:05:02

This question already has an answer here:

这个问题已经有了答案:

I have 2 buttons

我有2个按钮

 $(".button1").on("click", function(event){
 $(".button2").on("click", function(event){

What I would like to do is start with button1 as clicked and have no event listener, when button2 is clicked the event listener is removed and the event listener for button 1 is activated.

我想要做的是在单击时从button1开始,并且没有事件监听器,当单击button2时,事件监听器被移除,按钮1的事件监听器被激活。

This is a fairly common technique is in actionscript 3 with the method removeEventListener() and would like to use a similar method or technique in jquery.

这是一个相当常见的技术,在actionscript3中使用方法removeEventListener(),并希望在jquery中使用类似的方法或技术。

3 个解决方案

#1


7  

If you want to remove an event handler bound with jQuery.on then you probably want jQuery.off

如果您想删除与jQuery绑定的事件处理程序。然后你可能想要jQuery.off

http://api.jquery.com/off/

http://api.jquery.com/off/

#2


5  

on() method bind event and off() unbind it. If you give parameter to off() it will unbind only supplied event, but without parameter unbind all events.

on()方法绑定事件,off()解绑定事件。如果您将参数赋给off(),它将只解绑定提供的事件,但没有参数解绑定所有事件。

    $('.button1').on('click', function() {
        console.log('button1');
        $(this).off('click');
        $('.button2').on('click');
    })
    $('.button2').on('click', function() {
         console.log('button2');
        $(this).off('click');
        $('.button1').on('click');
    })

#3


0  

This is what I have but it does not put the event listener back on

这就是我所拥有的,但它没有重新启用事件监听器

 $(".slide1").on("click", function(){
    $(this).addClass('on');
    $('.slide2').removeClass('on');
     $(this).off('click');
    $('.slide2').on('click');
});

$(".slide2").on("click", function(){
    $(this).addClass('on');
    $('.slide1').removeClass('on');
    $(this).off('click');
    $('.slide1').on('click');
});

#1


7  

If you want to remove an event handler bound with jQuery.on then you probably want jQuery.off

如果您想删除与jQuery绑定的事件处理程序。然后你可能想要jQuery.off

http://api.jquery.com/off/

http://api.jquery.com/off/

#2


5  

on() method bind event and off() unbind it. If you give parameter to off() it will unbind only supplied event, but without parameter unbind all events.

on()方法绑定事件,off()解绑定事件。如果您将参数赋给off(),它将只解绑定提供的事件,但没有参数解绑定所有事件。

    $('.button1').on('click', function() {
        console.log('button1');
        $(this).off('click');
        $('.button2').on('click');
    })
    $('.button2').on('click', function() {
         console.log('button2');
        $(this).off('click');
        $('.button1').on('click');
    })

#3


0  

This is what I have but it does not put the event listener back on

这就是我所拥有的,但它没有重新启用事件监听器

 $(".slide1").on("click", function(){
    $(this).addClass('on');
    $('.slide2').removeClass('on');
     $(this).off('click');
    $('.slide2').on('click');
});

$(".slide2").on("click", function(){
    $(this).addClass('on');
    $('.slide1').removeClass('on');
    $(this).off('click');
    $('.slide1').on('click');
});