使用jquery无法鼠标在体内移动时出错?

时间:2012-11-23 15:49:07

标签: javascript jquery

我正在尝试demo here

在HTML中:

...
    <body>
       <div id="button-wrapper" style="position: absolute; opacity: 1; width: 27px; height: 20px;">
          <input type="submit" value="submit">
       </div>
    </body> 
...

在jQuery中:

$(document).ready(function(){
   $("#button-wrapper").parent().mousemove(function(e) {
       jQuery("#button-wrapper").css({
           top : e.pageY - 10,
           left : e.pageX + 30
       });
   });
});

当我在身体上移动鼠标时发生错误。提交按钮无法在鼠标移动事件上运行。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

试试这个:

$(function(){
    $(document).on('mousemove',function(e) {
        $("#button-wrapper").css({
            top : e.pageY - 10,
            left : e.pageX + 30
        });
    });
});

您应首先使用所有$(document),因为您尝试选择parent()的div bodythe document本身很容易on()。然后使用jQuery("#button-wrapper")进行实时DOM解释。

然后将此$("#button-wrapper")翻译为$(document).ready(function(){});只是因为如果您使用其中一个,请不要使用另一个。

同时将$(function(){});部分翻译为{{1}}。