[英]$('.photos ul li').length outputs 0 before i inject the photos and outputs five when they appear but when i remove them it still shows 5
when i click on the removePhotos button it will output the photos li length. 当我单击removePhotos按钮时,它将输出照片长度。 initially when the DOM loads their are no photos so it would output 0 and when i click show photos i use ajax to inject my photos.
最初,当DOM加载时,它们没有照片,因此它将输出0;当我单击“显示照片”时,我使用ajax注入照片。 after i insert my photos and click the removePhotos button it outputs 5 which is the number of photos i have but the problem is when i remove the photos and then click the removePhotos button it outputs 5 still when it should be outputting 0. any suggestions on how to fix this?
在我插入照片并单击removePhotos按钮后,它输出5,这是我所拥有的照片数量,但问题是当我删除照片时,然后单击removePhotos按钮,当它应输出0时仍输出5。如何解决这个问题?
my html 我的HTML
<div id="paris">
<h1>CRAPPY CHEESE</h1>
<button>Get Paris Photos</button>
<span>Remove Photos</span>
<ul class="photos">
</ul>
</div>
my javascript 我的JavaScript
function Tour(el) {
var tour = this;
this.el = el;
this.hidePhotos = function() {
alert($('.photos ul li').length);
$('.photos').fadeOut();
}
this.fetchPhotos = function() {
$.ajax('photos.html', {
data: {location: $("#tour").data(this.el)},
success: function(response) {
this.el.find('.photos').html(response).fadeIn();
},
context: tour,
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');
}
});
};
var kirt = this.el.on('click.showPhotos', 'button', this.fetchPhotos);
var pooh = this.el.on('click.removePhotos', 'span', this.hidePhotos);
}
$(document).ready(function() {
var paris = new Tour($('#paris'));
});
Pass into the jQuery fadeOut function a callback which does a $(this).remove() 将执行$(this).remove()的回调传递到jQuery fadeOut函数中
Your problem is, that fadeOut just hides your photos, but they are still present in the DOM. 您的问题是,fadeOut只会隐藏您的照片,但是它们仍然存在于DOM中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.