Пожалуйста, помогите, я не мог понять это.путать с ключевым словом «this» в jquery
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
внутри функции this.fetchPhotos
ссылаться на тур. Поэтому мой вопрос, почему эта часть кода tour.el.data('location')
изменится на this.el.data('location')
?
Спасибо расширенный за помощь
Read [ это] (http://learn.jquery.com/javascript-101/this-keyword/). –
вы теряете контекст 'this' внутри' $ .ajax', объявляете как другую переменную вне '$ .ajax' – charlietfl