如何在Moved函数中访问this.today?它将通过jQuery调用,因此this关键字将被jQuery覆盖为jQuery对象或DOM元素。
下面是与我所拥有的类似的东西:
(function(Map) {
Map.Timeline = {
today: null,
Init: function () {
jQuery("#timeline").mousemove(Map.Timeline.Moved); // or this.Moved
},
Moved: function (event) {
console.log(this); // jQuery Object or DOM element
console.log(this.today); // fails
console.log(Map.Timeline.today); // works fine
},
// more code here ...发布于 2013-04-21 18:11:47
使用jQuery.proxy()在回调调用中使用自定义上下文
jQuery('#timeline').mousemove(jQuery.proxy(Map.Timeline.Moved, this));发布于 2013-04-21 18:11:43
您可以在其被覆盖之前将其存储:
(function(Map) {
var myvar = $(this);
Map.Timeline = {
today: null,
Init: function () {
jQuery("#timeline").mousemove(Map.Timeline.Moved); // or this.Moved
},
Moved: function (event) {
console.log(myvar); // jQuery Object or DOM element
console.log(myvar.today); // fails
console.log(Map.Timeline.today); // works fine
},
// more code here ...https://stackoverflow.com/questions/16130040
复制相似问题