I m trying to understand, how jQuery plugins work. This is my first test plugin (it does nothing :) )
(function($)
{
var methods =
{
init : function(options)
{
if (options) $.extend(settings, options);
myElement = $(this);
return myElement;
},
getInfo : function()
{
console.log( Info = + myElement.attr( id ) + [ + settings.itemName + ] );
}
};
var settings = { itemName : item };
var myElement = null;
$.fn.myTest = function(method)
{
if (methods[method])
{
return methods[ method ].apply(this, Array.prototype.slice.call(arguments, 1));
}
else if (typeof method === object || ! method)
{
return methods.init.apply(this, arguments);
}
else
{
$.error( Method + method + does not exist on jQuery.myTag );
}
};
})(jQuery);
我在超文本中称:
$(document).ready(function()
{
t = $( #list ).myTest({itemName: First });
t.myTest( getInfo );
t2 = $( #l2 ).myTest({itemName: Hello World });
t2.myTest( getInfo );
t.myTest( getInfo );
});
我认为,假想的变数改变了我的特性:
Info = list [First] - OK
Info = l2 [Hello World] - OK
Info = l2 [Hello World] - Why???
What I need to write, that plugin variable myElement was different for each element? Thanks.