Javascript 检查是否存在图像

Javascript 检查是否存在图像,javascript,jquery,Javascript,Jquery,我正在使用.html()添加图像 $('#holder').html('<img class="holderClass" src="image.png" />'); 您可以检查图像元素的.length: if($('#holder > .holderClass').length > 0) { //Exists! } 如果要检查特定图像,请检查src属性: if($('#holder > .holderClass[src="path/to/img.png"]

我正在使用
.html()
添加图像

$('#holder').html('<img class="holderClass" src="image.png" />');

您可以检查图像元素的
.length

if($('#holder > .holderClass').length > 0) {
   //Exists!
}
如果要检查特定图像,请检查
src
属性:

if($('#holder > .holderClass[src="path/to/img.png"]').length > 0) {
   //Exists!
}

您正在使用
检查错误条件在您的代码中,这是错误的。
同样,对于jquery对象,您需要检查
length
以检查是否存在

 if($('.holderClass').length > 0){ 
   console.log('image.png is there');
 }
或者更具体地说

$('#holder > .holderClass').length > 0){ 
   console.log('image.png is there');
 } 
$('#holder > .holderClass').length > 0){ 
   console.log('image.png is there');
 }