Javascript 如何在插件中使用函数
我正在开发一个Javascript 如何在插件中使用函数,javascript,jquery,function,plugins,Javascript,Jquery,Function,Plugins,我正在开发一个JQuery插件。我对如何使用这个jQuery插件感到非常困惑。如何在插件外部调用jQuery插件函数 这是我的插件代码: (function ($) { function _setValue(self, value) { .... } //Other functions $.fn.inputpicker = function (method) { if (!this.length) return this;
JQuery插件
。我对如何使用这个jQuery插件感到非常困惑。如何在插件外部调用jQuery插件函数
这是我的插件代码:
(function ($) {
function _setValue(self, value) {
....
}
//Other functions
$.fn.inputpicker = function (method) {
if (!this.length) return this;
if (typeof method == 'object' || !method) {
return methods.init.apply(this, arguments);
}
else if (methods[method]) {
return methods[method].apply(this,
Array.prototype.slice.call(arguments, 1));
}
else { $.error("Method " + method + " does not exist on
jQuery.inputpicker"); }
}
});
你可以找到整个插件
现在,如何在我自己的代码中使用\u setValue
函数
现在,我如何在自己的代码中使用setValue函数
你可以写:
$('#test').inputpicker('val', 'your string');
调用inputpicker实例,并在此过程中传递函数和值
该值需要与数据属性之一匹配:
$(“#测试”).inputpicker({
数据:[“文本1”、“文本2”、“文本3”],
自动打开:正确
});
$('button')。在('click')上,函数(e){
$('#test').inputpicker('val',this.textContent);
})
文本1
文本2
Text 3
该函数与插件绑定,其行为类似于对象的私有方法。因此,您不能直接在插件外部访问该函数
您可以通过编辑插件来解决此问题,例如:
(function( $, document, window ) {
function _setValue(self, value) {
....
}
// export the function to the global window object
window.setValue = _setValue;
....
}( jQuery, document, window ));
完美:)谢谢你的帮助