jquery小部件全局但不是超全局变量(jquery widget global but not superglobal vars)

编程入门 行业动态 更新时间:2024-10-28 16:28:42
jquery小部件全局但不是超全局变量(jquery widget global but not superglobal vars)

我希望在拖动后访问变量,但是当我初始化窗口小部件时,$ this和$ opts将被覆盖并始终为'y'。 我如何存储和访问正确的变量?

$.widget("custom.test", { options: { map: '' }, _create: function(){ $this = this; $opts = $this.options; if($opts.map){ $this._createmap(); $this._loadmap(); } $this._test(); }, _test: function(){ $map = $this.map; $map.drag(function(e, dd) { // how to get the correct option here // $opts.map is to global? }); }, _createmap: function(){ $this.map = $('<div></div>').addClass('map'); }, }); $(x).test({map: 'x'}); $(y).test({map: 'y'});

谢谢!

I want to access vars after drag, but when i init the widget more times $this and $opts will be overwritten and is always 'y'. How can i store and access the correct vars?

$.widget("custom.test", { options: { map: '' }, _create: function(){ $this = this; $opts = $this.options; if($opts.map){ $this._createmap(); $this._loadmap(); } $this._test(); }, _test: function(){ $map = $this.map; $map.drag(function(e, dd) { // how to get the correct option here // $opts.map is to global? }); }, _createmap: function(){ $this.map = $('<div></div>').addClass('map'); }, }); $(x).test({map: 'x'}); $(y).test({map: 'y'});

Thanks!

最满意答案

有时它很容易

_test: function(){ $map = $this.map; $map.on('drag',{map: $opts.map}, function(e, dd) { // access with event.data alert(e.data.map); }); },

sometimes its so easy

_test: function(){ $map = $this.map; $map.on('drag',{map: $opts.map}, function(e, dd) { // access with event.data alert(e.data.map); }); },

更多推荐

本文发布于:2023-08-07 18:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465480.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:全局   部件   全局变量   jquery   vars

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!