问题
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