2013-06-25 50 views
0

首先,我想爲我的糟糕英語道歉。 在這個代碼塊:jquery - 阻止執行jquery塊中的下一行代碼

$("#div1").mouseleave(function(){ 
      $("#div2").mouseenter(function(){ 
       //To prevent hiding div2 
      }); 
      $("#div2").hide(); 
    }); 

正如你看到的我想阻止執行$("#div2").hide();如果光標DIV2輸入。

謝謝。

+2

不知道你要在這裏實現什麼,ü可以更具體 – dreamweiver

+0

嘗試檢查#div2'是否'是'事件的relatedTarget':http://api.jquery.com/event.relatedTarget/ – CBroe

回答

0

一個可行的辦法是通過一些像毫秒100,如果鼠標進入div2在其中取消隱藏操作

var div2HideTimer; 
$("#div1").mouseleave(function(){ 
    div2HideTimer = setTimeout(function(){ 
     $("#div2").hide(); 
    }, 100) 
}); 
$("#div2").mouseenter(function(){ 
    clearTimeout(div2HideTimer) 
}); 
+0

謝謝親愛的Arun P Johny!你的代碼工作。乾杯。 –

+0

@ user1425882 [你應該接受這個答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。這將標誌着問題解決。 –

1

如果我得到你的問題,你不想隱藏div2,如果離開div1時,鼠標立即進入div2或保持在div2中。

然後,你可以這樣做:

// returns true if the event is over the jQuery object o 
function eventIsOver(event, o) { 
    if ((!o) || o==null) return false; 
    var pos = o.offset(); 
    var ex = event.pageX; 
    var ey = event.pageY; 
    return (
     ex>=pos.left 
     && ex<=pos.left+o.width() 
     && ey>=pos.top 
     && ey<pos.top+o.height() 
    ); 
}; 

$("#div1").mouseleave(function(e){ 
      if (eventIsOver(e, $("#div2")) return; 
      $("#div2").hide(); 
}); 

請注意,根據您的具體情況,你可能需要做不同的事情。例如,如果兩個div之間可能存在差距(那麼您必須處理延遲),或者如果其中一個比另一個更重要,那麼您沒有精確。

1

能否請您試試這個拖延的div隱藏。我希望這是你所期望的。否則,讓我知道你想要什麼。

 $("#div1").mouseleave(function(){ 
     $("#div2").mouseenter(function(){ 
      //To prevent hiding div2 
     }); 
     if(!$("#div2").mouseenter()){ 
     $("#div2").hide(); 
     } 
    });