我想在动态创建的元素上使用 jQuery 的 hover()
。我尝试了以下但没有成功。我怎样才能做到这一点?我无法使用 mouseenter
和 mouseleave
因为此页面将嵌入到 iframe 中。
$(document).on('hover', '.dynamic', function(){
//do
});
最佳答案
hover()不是一个事件,它是一个用于注册 mouseenter 的实用方法和 mouseleave事件处理程序。
The .hover() method binds handlers for both mouseenter and mouseleave events. You can use it to simply apply behavior to an element during the time the mouse is within the element.
因此您可以对动态元素使用 mouseenter 和 mouseleave 事件处理程序
$(document).on('mouseenter', '.dynamic', function () {
//do
}).on('mouseleave', '.dynamic', function () {
//do
});
<小时/>
如果您想为两者使用一个处理程序,那么
$(document).on('mouseenter mouseleave', '.dynamic', function () {
//do
});
关于javascript - 在动态创建的元素上使用hover(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31237686/