2012-12-13 123 views
0

我有一個div元素,我想在鼠標輸入時觸發一個函數。 但它確實位於頁面的中心,因此在頁面加載和函數運行時,通常會在頁面的中心放置鼠標。 有沒有辦法讓它這樣我的功能不運行時,鼠標已經進入,但是當輸入?鼠標輸入事件,但只有當鼠標輸入不在

<div id = "mouse-control-div">.....</div> 
<script type="text/javascript"> 
    $('#mouse-control-div').mouseover(function() {alert('some thing');}); 
</script> 

我也試過.mouseenter但結果相同。

(代碼示例添加)

回答

0

如果你有一個布爾值,它會告訴你,如果你已經進入了?

有關jQuery的活動信息:鼠標懸停/鼠標移出主場迎戰的MouseEnter /鼠標離開look here

// When the DOM is ready, init scripts. 
jQuery(function($){ 
    var isEntered = false; 
    // Bind the mouse over /out to the first DIV. 
    $("#mouse-control-div").bind(
     "mouseover mouseout", 
     function(event){ 
      if(!isEntered) 
      { 
       isEntered = true; 
       echo('entered'); 
      } 
     } 
    ); 

    // Bind the mouse enter to the second DIV. 
    $("#mouse-control-div").bind(
     "mouseenter mouseleave", 
     function(event){ 
      if(isEntered) 
      { 
       isEntered = false; 
       echo('left'); 
      } 
     } 
    ); 

    function echo(string) 
    { 
     $('#output').append('<li>'+string+'</li>'); 
    } 

}); 

對於工作演示look here

+0

謝謝!當我評論_echo('進入'); _它按我的意思工作。 –

+0

如果你看看jsfiddle,你會看到我添加了'echo()'函數來顯示結果 –