我有10张.photoPreview类的图片库照片,我想知道如何检查有多少张src设置-例如:有多少个画廊图像存在。
$('.photoPreview').attr('src')我不知道如何使用Jquery获得这个值。也许可以使用find:
var number = find($('.photoPreview').attr('src'));谢谢你
HTML -如果没有设置图库图像,我的元素就会像这样开始:
<img src="" class="photoPreview" data-width="" data-height=""/>发布于 2013-11-09 05:27:14
您可以使用css选择器(如:not([src='']) )或jquery .not()。但是,如果您引用的是IMG元素的src属性,那么无论如何它们都需要属性集。
var number = $(".photoPreview:not([src=''])").length;
应起作用
发布于 2013-11-09 05:27:28
尝尝这个
1)获取具有类名photoPreview的图像总数。
( 2)在这些图像中循环。
var images = document.getElementsByClassName('photoPreview');
var imagesWithSrc =0;
for (var i=0; i<images.length; i++) {
if (images[i].src != '')
imagesWithSrc += 1;
}发布于 2013-11-09 05:27:20
通过使用jquery方法.each(),您可以为目标选择器的每个元素传递一个函数以执行一次。
允许你这样做:
var count = 0;
// loop through all elements in the array returned from the selector
$('.photoPreview').each(function(){
// feel free to customize your conditional
// to check the src however you like
if ( $(this).attr('src').length > 0 ) { count++; } // increment the count
});
alert(count);https://stackoverflow.com/questions/19872652
复制相似问题