通过 jQuery,on() 将 jQuery 插件调用附加到动态加载的元素
我有一部分代码是通过 AJAX 调用动态加载的,方法是将结果附加到父元素,类似于:
<div class="parent">
<!-- Inner content loaded dynamically -->
<div class="child">
</div>
<div class="child">
</div>
<!-- ... -->
</div>
现在,为了连接鼠标悬停事件,我会执行如下操作:
$(".parent").on("mouseenter", ".child", function(){
//Do fun stuff here
}
$(".parent").on("mouseleave", ".child", function(){
//Undo fun stuff here
}
这对于标准函数,但我想将其附加到第三方插件(在我的例子中,HoverIntent,但实际上任何插件) -
附加 HoverIntent 插件的语法如下:
$(".child").hoverIntent( makeTall, makeShort )
...但我希望它适用于文档最初加载时不可用的动态内容,以及类似 $(".parent") 的内容.on("hoverIntent", ".child", function(){});
似乎不是执行此操作的正确方法。
将插件应用于初始 $(document).ready()
之后加载的元素的正确方法是什么?
I have a portion of code that is dynamically loaded via an AJAX call by appending the result to a parent element, similar to this:
<div class="parent">
<!-- Inner content loaded dynamically -->
<div class="child">
</div>
<div class="child">
</div>
<!-- ... -->
</div>
Now, in order to hook up a mouseover event, I would do something like this:
$(".parent").on("mouseenter", ".child", function(){
//Do fun stuff here
}
$(".parent").on("mouseleave", ".child", function(){
//Undo fun stuff here
}
This works well enough for standard functions, but I want to attach this to a third-party plugin (in my case, HoverIntent, but really any plugin) -
The syntax for attaching the HoverIntent plugin is as so:
$(".child").hoverIntent( makeTall, makeShort )
... but I want this to work for my dynamic content that was not available at the time the document initially loaded, and something like $(".parent").on("hoverIntent", ".child", function(){});
doesn't seem to be the right way to do this.
What is the correct approach to applying a plugin to elements loaded after the initial $(document).ready()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jquery
.on
的工作原理是监视父对象上的事件,然后在事件源自匹配的子选择器时调用处理程序。然而,在您的情况下,您想要监视的事件是元素更改浏览器仅针对输入元素触发 onchange 事件(因为它们可以由用户更改)。
如果任何其他元素发生变化,那一定是因为 javascript,因此您可以在创建新内容后调用函数。
有 2 个实用的解决方案
1) 我通常做的是创建一个采用上下文(例如文档)的 init 方法。
然后我在更新 DOM 时手动调用 init (因为我更新 dom 时并不总是需要调用 init)
2)如果你真的需要监控一切,你可以使用这个插件
http://james.padolsey.com/javascript/monitoring-dom-properties/
然后
$('.parent').on('valuechange', function() { /* 初始化插件*/}
jquery
.on
works by monitoring events on a parent object and then calling the handler if the event originated from a matched child selector. In your case, however, the event you want to monitor is that the element changedBrowsers fire the onchange event for input elements only (because they can be changed by a user).
If any other element changes, it must be because of javascript, hence you can call functions after created new content.
There are 2 practical solutions
1) What i typically do is create an init method that takes a context (such as the document).
Then I manually call init when I update the DOM (because i dont always need to call init when I update the dom)
2) If you really need to monitor everything, you can use this plugin
http://james.padolsey.com/javascript/monitoring-dom-properties/
and then
$('.parent').on('valuechange', function() { /* init plugins*/}
我过去曾使用 jQuery Livequery 插件 来执行此操作
I have used the jQuery Livequery plugin in the past to do this
您可以使用 live 将事件附加到现在或将来位于 DOM 中的元素。
所以你的插件可能看起来像这样
,你可以调用这个插件
You can use live to attach event to element that will be in the DOM right now or in the future.
so your plugin could look like this
and you can call this plugin