[英]How to call a jquery function?
I have the following jQuery plugin: 我有以下jQuery插件:
(function ($) {
function _canvasScrollbar(c, s) {
...
}
_canvasScrollbar.prototype.myFunction = function(){
console.log("Yey!");
};
$.fn.canvasScrollbar = function (s) {
var a = {
handleColor: "#ccc",
scroll: 'horizontal',
sliderColor: "#eee"
};
$.extend(a, s);
return this.each(function () {
new _canvasScrollbar(this, a)
})
}
})(jQuery);
I initialize it like so: 我这样初始化它:
var sc = $(".myDiv").canvasScrollbar();
But, how do I call the function myFunction
once the plugin has been initialized? 但是,一旦插件初始化,我该如何调用函数
myFunction
?
ps I have tried $(".myDiv").canvasScrollbar("myFunction")
and sc.myFunction()
, but neither work. ps我尝试了
$(".myDiv").canvasScrollbar("myFunction")
和sc.myFunction()
,但都没有工作。
You need to store the new _canvasScrollbar
instance somewhere. 您需要在某处存储新的
_canvasScrollbar
实例。 The easiest way to do it with jQuery is using the data
method. 使用jQuery最简单的方法是使用
data
方法。
return this.each(function () {
$(this).data('scrollbar', new _canvasScrollbar(this, a));
})
Now you can access your instance using data
method. 现在,您可以使用
data
方法访问您的实例。 And then call the function. 然后调用该函数。
sc.data('scrollbar').myFunction();
// Yey!
You have two type of jQuery 'plugins': plugins
, which are usually stateless, and widgets
. 你有两种类型的jQuery'插件':
plugins
,通常是无状态的,以及widgets
。 You seem to be wanting a widget
, but you're creating it as a plugin
. 您似乎想要一个
widget
,但是您将其创建为plugin
。 See: 看到:
While most existing jQuery plugins are stateless – that is, we call them on an element and that is the extent of our interaction with the plugin – there's a large set of functionality that doesn't fit into the basic plugin pattern.
虽然大多数现有的jQuery插件都是无状态的 - 也就是说,我们在一个元素上调用它们,这就是我们与插件交互的程度 - 但是有一大组功能不适合基本的插件模式。
In order to fill this gap, jQuery UI has implemented a more advanced plugin system.
为了填补这一空白,jQuery UI实现了更高级的插件系统。 The new system manages state, allows multiple functions to be exposed via a single plugin, and provides various extension points.
新系统管理状态,允许通过单个插件公开多个功能,并提供各种扩展点。 This system is called the Widget Factory and is exposed as jQuery.widget as part of jQuery UI 1.8;
该系统称为Widget Factory,作为jQuery UI 1.8的一部分公开为jQuery.widget; however, it can be used independently of jQuery UI.
但是,它可以独立于jQuery UI使用。
Here: https://learn.jquery.com/plugins/stateful-plugins-with-widget-factory/ 这里: https : //learn.jquery.com/plugins/stateful-plugins-with-widget-factory/
To create a widget, you can do it like this: 要创建窗口小部件,您可以这样做:
$.widget('customWidget.canvasScrollbar', { options: { handleColor: "#ccc", scroll: 'horizontal', sliderColor: "#eee" }, _create: function() { }, myFunction: function() { console.log("Yey!"); } }); var sc = $("div").canvasScrollbar(); sc.canvasScrollbar('myFunction')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <div></div>
This way you have access to other features, such as sc.canvasScrollbar('instance')
, option setting, etc. 这样您就可以访问其他功能,例如
sc.canvasScrollbar('instance')
,选项设置等。
See: https://jqueryui.com/widget/ 请参阅: https : //jqueryui.com/widget/
Try defining pluginName
variable : "canvasScrollbar"
; 尝试定义
pluginName
变量: "canvasScrollbar"
; setiing _canvasScrollbar.prototype.myFunction
, this
at a
; setiing
_canvasScrollbar.prototype.myFunction
, this
在a
; defining settings
object as result of $.extend()
; 根据
$.extend()
定义settings
对象; setting c
.data(pluginName)
with s
object within _canvasScrollbar
; 在
_canvasScrollbar
使用s
对象设置c
.data(pluginName)
; returning this.element
from myFunction
for chaining jQuery methods to ; 从
myFunction
返回this.element
以将jQuery方法链接到; call _canvasScrollbar.prototype.myFunction
using .data(pluginName)
with dot notation $("div").canvasScrollbar().data("canvasScrollbar").myFunction()
调用
_canvasScrollbar.prototype.myFunction
使用.data(pluginName)
用点符号$("div").canvasScrollbar().data("canvasScrollbar").myFunction()
(function ($) { var pluginName = "canvasScrollbar"; function _canvasScrollbar(c, s) { $(c).data(pluginName, s) return c } _canvasScrollbar.prototype.myFunction = function(){ console.log("Yey!"); // return `this.element` : `this` for chaining jQuery // methods to `$(element).data("options").myFunction()` return this.element }; $.fn[pluginName] = function (s) { var a = { handleColor: "#ccc", scroll: "horizontal", sliderColor: "#eee", // set `myFunction` as property of `a`:`settings` myFunction: _canvasScrollbar.prototype.myFunction, // set `element` as `this` element: this }; var settings = $.extend(a, s); return this.each(function () { new _canvasScrollbar(this, settings) }) } })(jQuery); $("div").canvasScrollbar().data("canvasScrollbar").myFunction().css("color", "blue")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div>abc</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.