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); }); },更多推荐
发布评论