2012-12-13 212 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()'函数来显示结果 –