【发布时间】:2013-11-11 06:36:25
【问题描述】:
请帮忙,我想不通。
function Tour(el) {
var tour = this;
this.el = el;
this.fetchPhotos = function() {
$.ajax('/photos.html', {
data: {location: tour.el.data('location')},
context: tour,
success: function(response) {
this.el.find('.photos').html(response).fadeIn();
},
error: function() {
this.el.find('.photos').html('<li>There was a problem fetching the latest photos. Please try again.</li>');
},
timeout: 3000,
beforeSend: function() {
this.el.addClass('is-fetching');
},
complete: function() {
this.el.removeClass('is-fetching');
}
});
}
this.el.on('click', 'button', this.fetchPhotos);
}
$(document).ready(function() {
var paris = new Tour($('#paris'));
});
在上面的函数中,我知道context: tour 在this.fetchPhotos 函数中设置this 来引用Tour。所以我的问题是为什么这部分代码tour.el.data('location')可以改成this.el.data('location')?
感谢您的帮助
【问题讨论】:
-
阅读this。
-
您在
$.ajax中丢失了this的上下文,在$.ajax之外声明为不同的变量
标签: javascript jquery function object