mousemove事件在mousedown之后仅更新一次

时间:2014-04-30 09:32:28

标签: jquery mousemove mousedown

我在jquery中遇到了mousemove的问题。我想在mousedown事件和mousemove事件之后检查鼠标指针的坐标,但它只更新一次,结果只是mousedown事件时刻的坐标。

我真的需要一些建议,谢谢:)

我在这里只发布了原始代码的简短摘录:

P.S。我试图使用“$(document).ready(function(){”,但没有成功。

<!DOCTYPE html>
<head>
<title>jQuery Test</title>
   <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
   <script type="text/javascript" language="javascript">
    function my_func(event){
        $("#log1").html("mouse is down");
        var x = $(this);
        //some code
        $(document).ready(function(){
            x.on("mousemove", function(){
                $("#log1").html("x:"+event.pageX+" y:"+event.pageY);
            });
        });
    }

    $(document).ready(function(){
        $("button").on("mousedown",my_func); //it needs to be a named function because I will call it multiple times
    });
   </script>
</head>
<body>
   <button >TEST</button> 
   <div id="log1"></div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

更改

x.on("mousemove", function(){

x.on("mousemove", function(event){

JsFiddle - &gt; http://jsfiddle.net/6y83H/

答案 1 :(得分:0)

您是否忘记在鼠标移动功能上传递事件?

试试这个小提琴 fiddle

function my_func(event) {
    $("#first").html("mouse is moving</br>x:" + event.pageX + " y:" + event.pageY);
}

function my_func1(event) {
    $("#first").html("mouse is now down</br>x:" + event.pageX + " y:" + event.pageY);
}

$(document).ready(function () {
    $("#first").on("mousemove", my_func);
    $("#first").on("mousedown", my_func1);
});