双击需要激活binded mousedown / up

时间:2011-05-30 01:53:15

标签: javascript jquery

这是我的代码的一部分:

$('.toggle_box').each(function (){ 
    var timeout, longtouch;  
    $(this).bind('mousedown', function() {
        timeout = setTimeout(function() {
            longtouch = true;
        }, 1000);

    $(this).bind('mouseup', function(){
        if (!longtouch) {
            clearTimeout(timeout)
            var state = $(this).find('.switch').attr('data-state');
            if(state == "off"){
                $(this).find('.switch').attr('data-state','on');
            }
            else{
                $(this).find('.switch').attr('data-state','off');
            }
            var choice = $(this).parent();
            ckbmovestate(choice)
        }
        else{
            alert("3")
        };
    });
})
});

当我点击一个元素(mousedown和up)时,它工作正常,但是我需要双击该元素才能触发它。看起来第二次单击会重置绑定,以便3e可以再次使用它。只是很奇怪。

这是一个演示:不再有效...... 它是橙色复选框:)(请检查safari / chrome)

谢谢你的帮助:)

1 个答案:

答案 0 :(得分:1)

查看此处发布的代码... http://jsfiddle.net/qv4Ve/2/

您在mousedown事件处理程序中绑定了mouseup事件。你应该必须彼此独立地绑定它们。

$(this).bind('mousedown', function() {
    timeout = setTimeout(function() {longtouch = true;}, 1000);
    console.log("inside mouse down");
});

$(this).bind('mouseup', function(){
    console.log("inside mouse up");
    if (longtouch === false) {
        clearTimeout(timeout)
        var state = $(this).find('.switch').attr('data-state');
        if(state == "off"){
            $(this).find('.switch').attr('data-state','on');
        }
        else {
            $(this).find('.switch').attr('data-state','off')
        ;}

        var choice = $(this).parent();
        ckbmovestate(choice)
    }
    else {
        alert("is a long touch")
    };
});

上面发布的小提琴中的代码按照你想要的方式工作......

相关问题