5 回答

TA贡献1942条经验 获得超3个赞
(function() {
var time = 3;
var index = -1;
$('.divs div').bind('click', function() {
var _this = $(this);
var thisIndex = $('.divs div').index(_this);
if(time <= 0) {
time = 3;
index = -1;
}
if(index < 0) {
index = thisIndex;
timeCounter();
}
if(time > 0 && index != _this.index()) {
return false;
}
//
console.log(_this.text());
});
function timeCounter() {
var start = setTimeout(function() {
--time;
if(time < 0) {
clearTimeout(start);
return false;
}
setTimeout(arguments.callee, 1000);
}, 1000);
}
})();

TA贡献1803条经验 获得超6个赞
$(window).load(function(){ $(".divs div").bind("click",function(){ var item=$(this); setTimeout(function(){ $(".divs div").unbind("click"); alert("移出其他事件"); item.bind("click",function(){ alert("只有我能点"); }) },3000); }); });
HTML不变,将上面的代码加入js即可,我测试了,没问题。

TA贡献1744条经验 获得超4个赞
如果通过委托,则可以这样:
//通过事件委托来做(jQuery.fn.on) var lockDiv; $('.divs').on('click', 'div', function () { if (lockDiv != null && lockDiv != this) return; lockDiv = this; var self = $(this); alert('你点击的div的逻辑'); setTimeout(function () { lockDiv = null; }, 3000); });
考虑到性能,不要频繁的bind/unbind事件,通过在事件中判定是否禁止此次逻辑即可。
也可以这样:
//每个div绑定,适用于div各自事件逻辑不同 var cacheIndex = -1; $('.divs>div').each(function (i) { $(this).on('click', function () { if (cacheIndex !== -1 && cacheIndex != i) { alert('你的逻辑'); setTimeout(function () { cacheIndex = -1; }); } }); });
多使用jQuery.fn.on少使用jQuery.fn.bind,jQuery.fn.bind API已被废弃(好像是在1.7之后)
添加回答
举报