如何将.hover()绑定到动态创建的"li"卑鄙的? [英] How to bind .hover() to dynamically created "li" elemtent?

查看:83
本文介绍了如何将.hover()绑定到动态创建的"li"卑鄙的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够找到的所有解决方案都建议使用.live()方法.但是到今天为止,它已被弃用.

All the solutions I was able to find suggests to use .live() method. But as of today it is deprecated.

.hover()可以完美处理非动态创建的"li"元素.但是,一旦我添加了新的"li" .hover(),就根本不会触发.

.hover() works perfectly on "li" elements not created dynamically. But once I append new "li" .hover() is not triggered at all.

有人知道这个吗?

推荐答案

根据

The "hover" event has been deprecated with delegated event handling such as .on() per the .on() jQuery doc pages.

相反,您需要将.on()委托事件处理与mouseenter和mouseleave以及每个事件处理程序一起使用.

Instead, you need to use .on() delegated event handling with mouseenter and mouseleave and an event handler for each.

例如:

$(document).on("mouseenter", "li", function() {
    // hover starts code here
});

$(document).on("mouseleave", "li", function() {
    // hover ends code here
});

在实际代码中,您将选择一个静态父对象,该对象比document对象更接近动态li标签,以实现更好的性能.

In your real code, you would select a static parent object that is much closer to the dynamic li tags than the document object for better performance.

这篇关于如何将.hover()绑定到动态创建的"li"卑鄙的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆