jQuery:禁用另一个函数的操作

jQuery: disable action of another function

本文关键字:函数 操作 另一个 jQuery      更新时间:2023-12-20

我使用的是jQuery Toggles插件:https://github.com/simontabor/jquery-toggles

这是我的代码:

$(function(){
    $('.toggle').toggles({        
        clicker:'.i-am'
    });
    $('.i-am').click(function(){
        if ($(this).hasClass('active')){
            return false; /* And it doesn't stop the toggling */
        }        
    });
});

如您所见,我希望在单击.i-am.active时禁用切换。但return false;并没有起到作用。我怎样才能做到这一点?

更新。创建jsfiddle以明确:http://jsfiddle.net/webstyle/fBpvb/

试试这个:

http://jsfiddle.net/ydBm2/1/

$(function(){
    $('.i-am').click(function(e){
        if ($(this).hasClass('active')){
            e.stopImmediatePropagation();
            return false; /* This line doesn't work */
        }
        else {
            $('.i-am').removeClass('active');
            $(this).addClass('active');
        }
    });
    $('.toggle').toggles({
        drag:false,
        click: false, 
        text:{on:'',off:''},
        clicker:'.i-am'
    });
});