简体   繁体   English

绑定jQuery插件时使用委托目标

[英]Using a delegate target when binding for a jQuery plugin

I am writing a jQuery plugin. 我正在写一个jQuery插件。 When I am setting up the init method for this plugin, I want to set up events for the context, but am uncertain whether I should set a delegate target for the event or bind the event directly to the context. 当我为此插件设置init方法时,我想为上下文设置事件,但是不确定是为事件设置委托目标还是将事件直接绑定到上下文。

My main reason for wanting to consider a delegate is the use of this plugin with dynamic content. 我想考虑委托的主要原因是将此插件与动态内容一起使用。 In certain cases, these elements that the plugin is called on may be appended to the page using ajax. 在某些情况下,可以使用ajax将调用插件的这些元素附加到页面上。 In this case, is there a way to set the events up so that I don't have to destroy and re-bind the events after ajax calls? 在这种情况下,有没有办法设置事件,这样我就不必在ajax调用后销毁并重新绑定事件?

At the moment, I am doing something like this: 目前,我正在执行以下操作:

$(this).on('click', initEditor);

Should I consider doing something like this: 我应该考虑做这样的事情:

$('body').on('click', this, initEditor);

or event storing the delegate as an option in the options, like this: 或将委托作为选项存储在选项中的事件,如下所示:

$(options.delegate).on('click', this, initEditor);

I've noticed that, when using the delegate format, the event is bound to the context's selector, not the actual contextual element (ie if this is an anchor, it will bind to all anchors on the page). 我注意到,当使用委托格式时,事件绑定到上下文的选择器,而不是实际的上下文元素(即,如果是一个锚点,它将绑定到页面上的所有锚点)。 Any way to avoid that? 有什么办法避免这种情况?

Thanks! 谢谢!

If you need to bind only on children elements you will use this: 如果只需要绑定子元素,则可以使用以下方法:

$("#my_target").on("click", ".my_class_target", handler);

The ".on" event replaces "delegate, bind, live, etc". “ .on”事件替换为“代理,绑定,活动等”。 Now you can attach all events in only one place. 现在,您只能将所有事件附加在一个地方。 This is very nice, because before that you need to use "bind" that didnt work with ajax elements, for that you have the "live" wich is limited. 这非常好,因为在此之前,您需要使用对ajax元素不起作用的“绑定”,因为您的“实时”限制是有限的。

In my example the attach you work only with ".my_class_target" that is inside the "#my_target". 在我的示例中,附件仅与“ #my_target”内部的“ .my_class_target”一起使用。 So if attach the start element on body like: "$("body").on("click", ".my_class_target", handle) so all ".my_class_target" will be attached. Then, the secret is in your "selector". Have in mind that the first selector "$(#my_target")" have to be created when the page is loaded and the other selector can be loaded by ajax or created by dom. 因此,如果将起始元素附加到主体上,例如:“ $(” body“)。on(” click“,” .my_class_target“,handle),以便所有” .my_class_target“都将附加。然后,秘密就在您的”选择器“中“。请记住,在加载页面时必须创建第一个选择器” $(#my_target“)”,而另一个选择器可以由ajax加载或由dom创建。

Lets take a example: 让我们举个例子:

<div id="my_div">
    <ul class="ul-1">
        <li class="li-1-1"></li>
        <li class="li-1-2"></li>
        <li class="li-1-3"></li>
        <li class="li-1-4">
            <ul class="ul-2">
                <li class="li-2-1"></li>
            </ul>
        </li>
    </ul>
</div>

js: JS:

my_function = function(){};
$("body").on("click", "li", my_function); //handle all li
$("body").on("click", "ul.ul-1 > li", my_function); //attach only in li-1-X 

// if ul-1 is generate with ajax:
$("#my_div").on("click", "li", my_function); // will work with all li
$("ul.ul-1").on("click", "li", my_function); // wont work because the first selector "ul.ul-1" was not generated when the attach was called.

I hope this help. 希望对您有所帮助。

fonts: 字体:

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

http://api.jquery.com/category/selectors/ http://api.jquery.com/category/selectors/

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

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