鼠标输入事件但仅在鼠标输入时尚未输入

时间:2012-12-13 10:26:03

标签: html css

我有一个div元素,我想在鼠标进入时触发一个函数。 但它实际上位于页面的中心,因此鼠标通常在页面加载和函数运行时。 是否有一种方法可以使我的功能在鼠标已经进入但是进入时无法运行?

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

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

(已添加代码示例)

1 个答案:

答案 0 :(得分:0)

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

有关jQuery事件的信息:MouseOver / MouseOut与MouseEnter / MouseLeave 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

相关问题