我正在尝试这个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
});
});
});
当我在主体上移动鼠标时发生错误。提交按钮无法在鼠标移动事件上运行。我该如何解决这个问题?
最佳答案
试试这个:
$(function(){
$(document).on('mousemove',function(e) {
$("#button-wrapper").css({
top : e.pageY - 10,
left : e.pageX + 30
});
});
});
您应该首先使用所有$(document)
,因为您正在尝试选择div的parent()
,即body
,可以是轻松文档
本身。然后使用 on()
进行实时 DOM 解释。
然后将这个 jQuery("#button-wrapper")
翻译为 $("#button-wrapper")
因为如果你使用它,就不要使用另一个。
同时将 $(document).ready(function(){});
部分翻译为 $(function(){});
。
关于javascript - 使用jquery鼠标无法在body中移动时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13532241/