jquery插件方法之间的局部变量
我在快速使用的 jquery 插件上遇到了麻烦。 当调用 ajax 的启动句柄事件时,它会淡入,并在完成句柄时停止。
示例:
start : function (e,o) {
target.mask();
target.showMaskAjax();
},
complete : function (e,o) {
target.hideMaskAjax();....
},
问题是..当我触发 2 个 ajax 调用时,我想正确访问内部变量 “hideMaskAjax”已在
target.mask() 中的“target.mask()”中初始化,我一直在尝试:
this.mask = $("<div/>", {css: {display: "none"}).addClass("loading_mask");
this.spinner = $("<div/>", {css: {display: "none"}}).addClass("spinner");
在 target.hideMaskAjax() 中:
alert(this.mask); //fail
另外为此在插件中创建全局变量似乎不适用于多次调用。
I'm with trouble with a jquery plugin i've quickly playing around.
Its fades in when a start handle event of ajax is called and stops with complete handle.
Exemple:
start : function (e,o) {
target.mask();
target.showMaskAjax();
},
complete : function (e,o) {
target.hideMaskAjax();....
},
The question is..when I fire 2 ajax calls I want to correctly have access of variables inside
"hideMaskAjax" where have been initialize in "target.mask()"
in target.mask() i've been trying:
this.mask = $("<div/>", {css: {display: "none"}).addClass("loading_mask");
this.spinner = $("<div/>", {css: {display: "none"}}).addClass("spinner");
And in target.hideMaskAjax():
alert(this.mask); //fail
Also creating global variables in plugin for this seems dont work for more than one invokes of it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们确实需要查看插件代码的其余部分来了解您是如何做到的。这就是说,在我看来,您需要创建一个实际的对象,以便可以拥有成员变量和多个实例。然后你可以将东西附加到上面。
Wed really need to see the rest of the plugin code to see how youre doing it. That said it sounds to me like you need to create an actual Object so that you can have member variables and multiple instances. Then you can attach things to it.
在我看来,不要把事情搞得太复杂。最简单、最快、最脏的解决方案是将变量直接放入目标中。这两个函数都可以像这样访问目标。选择一个不常见的名字。
顺便说一句,您的警报(this.mask)可能会失败,因为 mask 已经被声明为目标上的函数(因此您需要小心命名)。尝试在前面加上下划线。
Don't overcomplicate things, imo. Your simplest, quickest and dirtiest solution is to put your variable directly in target. Both functions have access to target as this. Choose an uncommon name.
Your alert(this.mask) is probably failing, btw because mask is already declared as a function on target (hence why you need to be careful with naming). Try prepending with underscores.