0

我正在使用以下插件:https ://github.com/richardscarrott/jquery-mousestop-event

我有一个使用 JavaScript 动态注入 DOM 的元素,我想将“mousemove”事件附加到该元素:

$(document).on('mousestop', '.zoomImg', function(e) {
       // code goes here
});

这不起作用,有什么建议吗?

问候,克里斯

4

1 回答 1

1

除非它存在于 DOM 中,否则您不能将事件绑定到元素,因此您需要在确定它存在后进行绑定。请记住,JavaScript 是异步的并且具有函数作用域,因此如果您将事件绑定到注入元素的同一个函数中,它应该可以工作。

function injectElement() {
  // Inject the element
  ...
  // Bind the event
  ...
}
于 2013-06-04T14:51:20.113 回答