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之后)
?
?
添加回答
舉報