简体   繁体   English

通过jQuery,on()将jQuery插件调用附加到动态加载的元素

[英]Attaching jQuery plugin calls to dynamically loaded elements via jQuery,on()

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: 我有一部分代码通过AJAX调用动态加载,方法是将结果附加到父元素,类似于:

<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) - 这对标准函数来说效果很好,但我想将它附加到第三方插件(在我的例子中, HoverIntent ,但实际上是任何插件) -

The syntax for attaching the HoverIntent plugin is as so: 附加HoverIntent插件的语法如下:

$(".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(){}); ...但我希望这适用于我最初加载文档时不可用的动态内容,以及$(".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() ? 将插件应用于初始$(document).ready()之后加载的元素的正确方法是什么?

jquery .on works by monitoring events on a parent object and then calling the handler if the event originated from a matched child selector. jquery .on通过监视父对象上的事件,然后在事件源自匹配的子选择器时调用处理程序来工作。 In your case, however, the event you want to monitor is that the element changed 但是,在您的情况下,您要监视的事件是元素已更改

Browsers fire the onchange event for input elements only (because they can be changed by a user). 浏览器仅为输入元素触发onchange事件(因为它们可以由用户更改)。

If any other element changes, it must be because of javascript, hence you can call functions after created new content. 如果任何其他元素发生变化,则必须是因为javascript,因此您可以在创建新内容后调用函数。

$(".child", parentElementContext).hoverIntent( makeTall, makeShort )

There are 2 practical solutions 有两个实用的解决方案

1) What i typically do is create an init method that takes a context (such as the document). 1)我通常做的是创建一个采用上下文(如文档)的init方法。

MyPage.init = function(context) {
    $('.selector', context).hoverIntent();
    $('.other', context).dialog(); // any other plugins
};

Then I manually call init when I update the DOM (because i dont always need to call init when I update the dom) 然后我在更新DOM时手动调用init(因为我在更新dom时总是不需要调用init)

$.ajax({
  url: url,
  data: data,
  success: function(data){ 
     var context = $('.parent'); 
     context.html(data);
     MyPage.init(context); //calls hoverIntent and other plugins
  }
});

2) If you really need to monitor everything, you can use this plugin http://james.padolsey.com/javascript/monitoring-dom-properties/ 2)如果你真的需要监控一切,你可以使用这个插件http://james.padolsey.com/javascript/monitoring-dom-properties/

and then $('.parent').on('valuechange', function() { /* init plugins*/} 然后是$('.parent').on('valuechange', function() { /* init plugins*/}

我过去使用过jQuery Livequery插件来做到这一点

You can use live to attach event to element that will be in the DOM right now or in the future. 您可以使用live将事件附加到现在或将来在DOM中的元素。

$(".parent .child").live("mouseover", function(){
    //Do fun stuff here
}).live("mouseout", function(){
    //Undo fun stuff here
});

so your plugin could look like this 所以你的插件看起来像这样

$.fn.hoverIntent = function(mouseover, mouseout) {
    this.live("mouseover", mouseover).live("mouseout", mouseout);
    return this;
};

and you can call this plugin 你可以调用这个插件

$(".parent .child").hoverIntent(makeTall, makeShort).load("other_page.php");

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

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