在事件发生dom更改后调用jquery函数 [英] Call a jquery function after dom changed with an event

查看:98
本文介绍了在事件发生dom更改后调用jquery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单击事件后生成了以下jquery clickno类.它是click事件后生成的popover中的类.

I have the following jquery clickno class generated after a click event. It is a class in popover generated after a click event.

$(".clickno").on('click', function(){$(this).parent().parent().parent().hide()})

因此,当我将其添加到脚本中时,它将无法正常工作,因为在加载DOM时没有clickno类.弹出窗口加载后如何调用此函数.

So when I add this to my scripts, it is not working because there is no clickno class when DOM loaded. How can i call this function after the popover loaded.

推荐答案

如果将该事件委托给.clickno的父级,或者将该事件委托给文档,也可以为您做这件事.

Delegate the event to parent of .clickno if you know it parent or delegating the event to document could also do that for you.

$(document).on('click', ".clickno", function()
    {$(this).parent().parent().parent().hide()
});

委派的事件

委派事件具有可以处理以下事件的优势: 后继元素添加到文档中的子元素.经过 选择一个保证在当前时间存在的元素 附加了委托事件处理程序,您可以使用委托事件来 避免需要频繁地附加和删除事件处理程序, 参考.

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, reference.

这篇关于在事件发生dom更改后调用jquery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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