jQuery plugin: How to access elements where plugin was applied from secondary function?

老子叫甜甜 提交于 2019-12-24 08:47:17

问题


I am new to jQuery and I am building a custom plugin, it looks something like this (pseudo-code):

jQuery.fn.myPlugin = function( options )
{

    var defaults = {
        interval : 5 * 1000
    };

    var interval_handler = setInterval( function( ) { update( ); }, interval );

    var opts = $.extend( defaults, options );

    return this.each( function( ){
        $( this ).bind( event, stuff );
    });

    function update( )
    {
        if ( condition == true )
        {
            clearInterval( interval );

            // unbind() foreach element the plugin has used
        }
    }
}

My question is:

How can access all the elements the plugin has used on return this.each(...) from the update( ) function?

Also, is the way im using functions inside the plugin correct? I didn't know how to do it so I just tried that and it worked.


回答1:


You need to put it in a variable:

var elements = this;


来源:https://stackoverflow.com/questions/7815802/jquery-plugin-how-to-access-elements-where-plugin-was-applied-from-secondary-fu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!