I'm pulling my hair out trying to figure out why the mouseover event won't work with the .on handler with a dynamically created element from ajax. The only thing that seems to work is the code with .live but I understand that it is deprecated.
我正在试着弄清楚为什么mouseover事件不能与.on处理程序一起使用ajax动态创建的元素。似乎唯一有效的是带有.live的代码,但我知道它已被弃用。
$(".dropdown ul li").live("mouseover", function() {
alert('mouseover works');
});
However, when I try using .on, it will not work - no matter what variations I try (document.ready, .mouseover, etc etc)
但是,当我尝试使用.on时,无论我尝试什么变化(document.ready,.mouseover等等)都无法使用。
$(".dropdown ul li").on("mouseover", function() {
alert('mouseover works');
});
The event handlers are at the bottom of the code, so they are executed last. Anyone have an idea of what I'm doing wrong??
事件处理程序位于代码的底部,因此它们最后执行。任何人都知道我做错了什么?
1 个解决方案
#1
21
Using .on
for newly generated elements with dynamic event delegation http://api.jquery.com/on/ - where your main selector is an existent static parent:
对于具有动态事件委派的新生成元素使用.on http://api.jquery.com/on/ - 其中主选择器是现有的静态父级:
$(".static-parent").on("event1 event2", ".dynamic-child", function() {
or in your case:
或者在你的情况下:
$(".dropdown").on("mouseover", "li", function() {
alert('mouseover works!!!!!!!!!');
});
Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.
委托事件的优点是,它们可以处理来自稍后添加到文档的后代元素的事件。通过选择在附加委托事件处理程序时保证存在的元素,您可以使用委派事件来避免频繁附加和删除事件处理程序。例如,此元素可以是模型 - 视图 - 控制器设计中视图的容器元素,如果事件处理程序想要监视文档中的所有冒泡事件,则可以是文档。在加载任何其他HTML之前,文档元素在文档的头部可用,因此可以安全地将事件附加到那里而无需等待文档准备就绪。
Also make sure to use a DOM ready function
还要确保使用DOM就绪功能
jQuery(function($) { // DOM is now ready and $ alias secured
$(".dropdown").on("mouseover", "li", function() {
alert('mouseover works!!!!!!!!!');
});
});
#1
21
Using .on
for newly generated elements with dynamic event delegation http://api.jquery.com/on/ - where your main selector is an existent static parent:
对于具有动态事件委派的新生成元素使用.on http://api.jquery.com/on/ - 其中主选择器是现有的静态父级:
$(".static-parent").on("event1 event2", ".dynamic-child", function() {
or in your case:
或者在你的情况下:
$(".dropdown").on("mouseover", "li", function() {
alert('mouseover works!!!!!!!!!');
});
Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.
委托事件的优点是,它们可以处理来自稍后添加到文档的后代元素的事件。通过选择在附加委托事件处理程序时保证存在的元素,您可以使用委派事件来避免频繁附加和删除事件处理程序。例如,此元素可以是模型 - 视图 - 控制器设计中视图的容器元素,如果事件处理程序想要监视文档中的所有冒泡事件,则可以是文档。在加载任何其他HTML之前,文档元素在文档的头部可用,因此可以安全地将事件附加到那里而无需等待文档准备就绪。
Also make sure to use a DOM ready function
还要确保使用DOM就绪功能
jQuery(function($) { // DOM is now ready and $ alias secured
$(".dropdown").on("mouseover", "li", function() {
alert('mouseover works!!!!!!!!!');
});
});