jQuery .hover不适用于AJAX呈现的元素 [英] jquery .hover not working on AJAX rendered elements

查看:166
本文介绍了jQuery .hover不适用于AJAX呈现的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些通过AJAX调用创建的元素.在这些元素中,有一个子元素,将其悬停时需要显示动态创建的另一个子元素.当我在小提琴中运行.hover jQuery时,它运行良好.当我在代码中实现它时,它并不想工作.

I have some elements that are created from an AJAX call. Within those elements, there is a child element that when hovered needs to show another child element that was dynamically created. When I run the .hover jquery in a fiddle, it works fine. When I implement it in my code, it doesn't want to work.

我想知道这是否取决于何时加载.hover脚本与何时从AJAX加载元素.一个人需要先走吗?在运行.hover脚本之前,是否应该制定.promise来等待AJAX​​元素加载?

I was wondering if it depends on when the .hover script is loaded vs when the elements are loaded from AJAX. Does one need to go before the other? Should a .promise be enacted to wait for the AJAX elements to load before the .hover script is run?

这是我的示例的小提琴.

推荐答案

我理解您的问题是指您将事件附加到可能尚不存在的元素.而不是绑定到元素(它是.hover()的缩写),您应该将其委派给永久性容器元素.您可以使用delegate().on()来执行此操作,具体取决于jQuery版本.

I understand your question to mean that you are attaching an event to an element that may not exist yet. Instead of binding to the element (which .hover() is shorthand for) you should delegate it to a permanent container element. You can do this with either delegate() or .on() depending on jQuery version.

这是您的提琴中的样子: http://jsfiddle.net/7dcuh/15/

Here's what it would look like in your fiddle: http://jsfiddle.net/7dcuh/15/

这篇关于jQuery .hover不适用于AJAX呈现的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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