这是我的代码的一部分:
$('.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")
};
});
})
});
当我点击一个元素(鼠标向下和向上)时,它工作得很好,但第二次我必须双击该元素才能激发它。第二次点击似乎重置了绑定,以便3e可以再次使用它。真奇怪。
这是一个演示:不再有效。。。这是橙色复选框:)(请在safari/chrome中进行检查)
感谢您的帮助:)
签出此处发布的代码。。。http://jsfiddle.net/qv4Ve/2/
您正在将mouseup事件绑定到mousedown事件处理程序中。你应该把它们彼此独立地捆绑在一起。
$(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")
};
});
上面发布的fiddle中的代码按照你想要的方式工作。。。