我有一个简单的 jQuery 插件,我希望能够在单个页面上调用多个元素。
到目前为止,我明确声明了选择器,但我希望它们与调用插件的对象相关。我怎样才能做到这一点?
(function($) {
var MyPlugin = function(options){
var plugin = $(this); // The entire myPlugin object
var element = $("div#plugined div#element"); // An element in myPlugin
var pluginChild = $( ); // Need selector of what plugin was called on + additional slider. ("div#plugined div#child")
this.publicFunction = function() {
// something...
}
var privateFunction = function() {
// something private...
}
};
$.fn.myPlugin = function(options) {
var defaults = {
// options
}
var options = $.extend(defaults, options);
var myPlugin = new MyPlugin(options);
$("div#plugined a.arrow").bind('click', function(e){ // Same problem. Need selector of what plugin was called on + additional slider. ("div#plugined div#child")
e.preventDefault();
myPlugin.publicFunction();
});
return myPlugin;
};
})(jQuery);
<script type="text/javascript">
$(window).load(function() {
$('div#plugined').myPlugin();
});
</script>