我在课堂上有10张图库照片.photoPreview我想知道如何检查有多少src设置 - 例如:有多少图库图像。
$('.photoPreview').attr('src')
我不确定如何使用Jquery获取此值。也许使用find:
var number = find($('.photoPreview').attr('src'));
三江源
HTML - 如果没有设置图库图像,我的元素就像这样开始:
<img src="" class="photoPreview" data-width="" data-height=""/>
答案 0 :(得分:4)
您可以使用css选择器,例如:not([src=''])
或jquery equiv .not()
。如果您指的是IMG元素的src
属性,则无论如何都需要设置属性。
var number = $(".photoPreview:not([src=''])").length;
应该工作
答案 1 :(得分:1)
试试这个
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;
}
答案 2 :(得分:0)
通过使用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);