我无法从privat方法读取jQuery-UI小部件工厂选项 [英] I can't read jQuery-UI widget factory options from a privat method
问题描述
我是jQueryUI小部件工厂的新手,因此我可能会问这很愚蠢. 我想创建一个ajax工具提示,可以在其中通过一个选项设置ajax调用的网址,但是此选项在包含ajax调用的_mouseOver方法中不可读.
I'm new with the jQueryUI widget factory, therefore it may be silliness what I'll ask. I'd like to create an ajax tooltip where I can set the url of the ajax call through an option but this option is not readable in the _mouseOver method which contains the ajax call.
(function($) {
$.widget("ui.tooltip", {
options: {
url: ''
},
_create: function() {
alert(this.options.url); //it works
this.element.bind({
mouseenter: this._mouseOver
});
},
_mouseOver: function() {
alert(this.options.url); //it dosen't works
},
...
在我设置时: $(.text").tooltip({url:"something"});
As I setup: $(".text").tooltip({url: "something" });
请有人帮我.
推荐答案
(function ($) {
$.widget("ui.tooltip", {
self: null,
options: {
url: ''
},
_create: function () {
self = this;
self.element.bind({
mouseenter: self._mouseOver
});
},
_mouseOver: function () {
alert(self.options.url); // it should work
}
});
})(jQuery);
在_mouseOver中使用"this"是指事件函数中的当前对象,而不是指小部件本身.您应该创建一个变量,然后在其上放置小部件,以便在任何事件或方法中都可以使用它. 在jQuery中使用$ .each()函数时,您会发现相同的行为.
Using "this" inside _mouseOver refers to a current object inside the event function and not to the widget itself. You should create a variable and put the widget (this) on it, to be able to use it options in any event or method. You will find the same behavior when using a $.each() function in jQuery.
这篇关于我无法从privat方法读取jQuery-UI小部件工厂选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!