问题
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