Hiding images with blank src using jquery

谁都会走 提交于 2019-12-14 03:55:44

问题


I have 4 sub images on a page that get their src attribute from a database. they all have a class="subImage". When there is no DB entry I would like to hide the element as opposed to have a broken link as i currently have. I have tried in jQuery:

<script>
$(document).ready(function() {
    $('.subImage[src^=""]').css('visibility:hidden');
});
</script>

Am I way off?

thanks.


回答1:


You pass parameters to the css function as follows:

$(selector).css('visibility','hidden')

or

$(selector).css({'visibility':'hidden', 'newAttr':'newValue'})



回答2:


Try hiding them

  $("img").error(function(){
       $(this).hide();
  });



回答3:


A better way to do this would be with CSS. You can use the attribute selector to select an element based on the value of it's attribute. Like so.

.subImage[src=""] { /* Selects all .subImage where the src is blank. */
    visibility: hidden;
}



回答4:


You could do

$(document).ready(function() {
    $('.subImage').filter(function(){ return this.src === ''}).hide();
});



回答5:


This should do it:

$(document).ready(function() {
    $('img[src=""]').hide();
});


来源:https://stackoverflow.com/questions/10447031/hiding-images-with-blank-src-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!