0

我有一个 div 元素和一个我想在鼠标进入它时触发一个函数。但它确实位于页面的中心,因此鼠标通常在页面加载和功能运行时位于其上。有没有办法让它在鼠标已经进入但进入时我的功能不会运行?

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

我也试过 .mouseenter 但结果相同。

(添加了代码示例)

4

1 回答 1

0

如果你有一个布尔值告诉你是否已经输入了怎么办?

有关 jQuery 事件的信息:MouseOver / MouseOut vs. MouseEnter / MouseLeave 看这里

// 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>');
    }

});

​</p>

对于工作演示看这里

于 2012-12-13T11:35:29.753 回答