Javascript jQuery:如何使对象中的函数公开?
我使用一个分页插件,我想公开它的一个函数,这样我就可以从“外部”调用它。我无法使用Javascript jQuery:如何使对象中的函数公开?,javascript,jquery,Javascript,Jquery,我使用一个分页插件,我想公开它的一个函数,这样我就可以从“外部”调用它。我无法使用pageSelected($(obj))直接调用它 有没有一种简单的方法使它可见,这样我就可以称之为 Jerrywindow是JavaScript中的全局对象,因此可以全局使用window.myfunc=myfunc和myfunc 请看一看这方面的实例: // Expose jQuery to the global object window.jQuery = window.$ = jQuery; window是
pageSelected($(obj))
直接调用它
有没有一种简单的方法使它可见,这样我就可以称之为
Jerry
window
是JavaScript中的全局对象,因此可以全局使用window.myfunc=myfunc
和myfunc
请看一看这方面的实例:
// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;
window
是JavaScript中的全局对象,因此可以全局使用window.myfunc=myfunc
和myfunc
请看一看这方面的实例:
// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;
您还可以使用更面向对象的方式来编写插件 赫克托·维根(Hector Virgen)在他的博客上发布了一个很棒的方法: 插件代码:
(function($){
var MyPlugin = function(element, options)
{
var elem = $(element);
var obj = this;
var settings = $.extend({
param: 'defaultValue'
}, options || {});
// Public method - can be called from client code
this.publicMethod = function()
{
console.log('public method called!');
};
// Private method - can only be called from within this object
var privateMethod = function()
{
console.log('private method called!');
};
};
$.fn.myplugin = function(options)
{
return this.each(function()
{
var element = $(this);
// Return early if this element already has a plugin instance
if (element.data('myplugin')) return;
// pass options to plugin constructor
var myplugin = new MyPlugin(this, options);
// Store plugin object in this element's data
element.data('myplugin', myplugin);
});
};
})(jQuery);
$('#test').myplugin();
var myplugin = $('#test').data('myplugin');
myplugin.publicMethod(); // prints "publicMethod() called!" to console
调用插件:
(function($){
var MyPlugin = function(element, options)
{
var elem = $(element);
var obj = this;
var settings = $.extend({
param: 'defaultValue'
}, options || {});
// Public method - can be called from client code
this.publicMethod = function()
{
console.log('public method called!');
};
// Private method - can only be called from within this object
var privateMethod = function()
{
console.log('private method called!');
};
};
$.fn.myplugin = function(options)
{
return this.each(function()
{
var element = $(this);
// Return early if this element already has a plugin instance
if (element.data('myplugin')) return;
// pass options to plugin constructor
var myplugin = new MyPlugin(this, options);
// Store plugin object in this element's data
element.data('myplugin', myplugin);
});
};
})(jQuery);
$('#test').myplugin();
var myplugin = $('#test').data('myplugin');
myplugin.publicMethod(); // prints "publicMethod() called!" to console
您还可以使用更面向对象的方式来编写插件 赫克托·维根(Hector Virgen)在他的博客上发布了一个很棒的方法: 插件代码:
(function($){
var MyPlugin = function(element, options)
{
var elem = $(element);
var obj = this;
var settings = $.extend({
param: 'defaultValue'
}, options || {});
// Public method - can be called from client code
this.publicMethod = function()
{
console.log('public method called!');
};
// Private method - can only be called from within this object
var privateMethod = function()
{
console.log('private method called!');
};
};
$.fn.myplugin = function(options)
{
return this.each(function()
{
var element = $(this);
// Return early if this element already has a plugin instance
if (element.data('myplugin')) return;
// pass options to plugin constructor
var myplugin = new MyPlugin(this, options);
// Store plugin object in this element's data
element.data('myplugin', myplugin);
});
};
})(jQuery);
$('#test').myplugin();
var myplugin = $('#test').data('myplugin');
myplugin.publicMethod(); // prints "publicMethod() called!" to console
调用插件:
(function($){
var MyPlugin = function(element, options)
{
var elem = $(element);
var obj = this;
var settings = $.extend({
param: 'defaultValue'
}, options || {});
// Public method - can be called from client code
this.publicMethod = function()
{
console.log('public method called!');
};
// Private method - can only be called from within this object
var privateMethod = function()
{
console.log('private method called!');
};
};
$.fn.myplugin = function(options)
{
return this.each(function()
{
var element = $(this);
// Return early if this element already has a plugin instance
if (element.data('myplugin')) return;
// pass options to plugin constructor
var myplugin = new MyPlugin(this, options);
// Store plugin object in this element's data
element.data('myplugin', myplugin);
});
};
})(jQuery);
$('#test').myplugin();
var myplugin = $('#test').data('myplugin');
myplugin.publicMethod(); // prints "publicMethod() called!" to console
非常感谢。我也尝试过这个方法,但它对我不起作用(方法)。它说我调用这个方法时没有定义myplugin-(别忘了在正文中的某个地方包含#test元素。在普通文件中尝试此方法,它应该会起作用:谢谢。我也尝试过此方法,但对我不起作用(方法)。它说我调用此方法时未定义myplugin;-(别忘了在正文中的某个地方包含#test元素。在普通文件中尝试此方法,它应该会起作用: