if ...图像空源(jQuery)

时间:2012-02-22 17:33:42

标签: jquery asp.net hide show

在我的应用程序(asp.net)中,我有一个图像控件,默认情况下它有一个空源。当用户单击缩略图时,将在该图像控件中打开单击的图像。问题是可以查看的图像是由用户上传的,因此我无法设置默认图像(因为加载页面时不确定图像是否存在)。

问题在于,因为加载页面时图像src为空,所以会显示损坏的图像链接。单击缩略图时一切正常。

我提出的一个解决方案是使用jQuery来检查图像是否有空src,如果是,则隐藏它。

下面是我为此编写的代码,但由于某种原因没有任何反应。什么可能是错的(当页面完成GET时加载代码?)

if ($("#fullSizeImage").attr(src="") == true) {
    $("#fullSizeImage").hide();
}
else {
    $("#fullSizeImage").show();
}

HTML:

<div id="fullImageArea"> 
    <img id="fullSizeImage" src="" /> 
</div>

2 个答案:

答案 0 :(得分:2)

您的验证应该是:

if ($("#fullSizeImage").attr('src') == "") {

这是因为您使用.attr()的方式错误,根据jQuery's documentation:,您可以通过以下方式使用.attr():

.attr( attributeName ) 

描述:获取匹配元素集中第一个元素的属性值。

或者

.attr( attributeName, value ) 

描述:为匹配元素集设置一个或多个属性。

答案 1 :(得分:1)

if (!$("#fullSizeImage").attr('src')) {
  $("#fullSizeImage").hide();
}
else {
   $("#fullSizeImage").show();
}
​

请使用以上内容。