简体   繁体   English

绑定通过AJAX加载的内容

[英]Binding content loaded via AJAX

We have a WordPress plugin, which dynamically - via AJAX calls - loads user specific content (html) to a page. 我们有一个WordPress插件,该插件可通过AJAX调用动态地将用户特定的内容(html)加载到页面上。 However, WordPress theme and other plugin developers increasingly process(animation etc.) elements, using a combination of javascript and CSS, which exist in the DOM by document.ready() . 但是,WordPress主题和其他插件开发人员越来越多地使用JavaScript和CSS的组合来处理(动画等)元素,这些元素通过document.ready()存在于DOM中。

We don't have control over what they do or how they do it. 我们无法控制他们做什么或如何做。 If we did we'd insist on using .on() and .trigger() event delegation and packaging things up in functions. 如果这样做,我们将坚持使用.on().trigger()事件委托并将其包装在函数中。

Is there something that WE can do that ensures that all content introduced after document.ready() gets properly bound for clicks, animations etc? WE可以做些什么来确保将document.ready()之后引入的所有内容正确地绑定到单击,动画等方面?

Depending on your jquery version, you can handle delegated events (events for elements added after the first "on" method call) with either "delegate" (for jquery < 1.7) or "on" (for jquery >= 1.7) methods. 根据您的jquery版本,您可以使用“ delegate”(对于jquery <1.7)或“ on”(对于jquery> = 1.7)方法来处理委托事件(在第一次“ on”方法调用之后添加的元素的事件)。

You have to use this form, though: 但是,您必须使用此表格:

$(".containerElement").on( 'click' , '.clickableElement', function(e){
    //Do stuff
});

Delegate function has the same arguments. 委托函数具有相同的参数。

More info on events and event delegation on: 有关事件和事件委托的更多信息:

http://api.jquery.com/on/ http://api.jquery.com/on/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM