if... 具有空源的图像 (jQuery)
在我的应用程序(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>
In my application (asp.net) I have an image control which by default has an empty source. When a user clicks on a thumbnail, the clicked image is opened in that image control. The thing is that the images that can be viewed is uploaded by the user, and by that reason I can't set a default image (because it's not for sure that an image exists when the page is loaded).
The problem is that because the image src is empty when loading the page, a broken image link is displayed. When a thumbnail is clicked everything works fine.
One solution of this that I came up with is using jQuery to check if the image has an empty src, and if so hide it.
Below is the code that i've written for that, but by some reason nothing happens. What can be wrong (the code is loaded when a GET is done by the page)?
if ($("#fullSizeImage").attr(src="") == true) {
$("#fullSizeImage").hide();
}
else {
$("#fullSizeImage").show();
}
HTML:
<div id="fullImageArea">
<img id="fullSizeImage" src="" />
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的验证应该是这样的:
那是因为您使用
.attr()
的方式错误,根据 jQuery 文档: 您可以通过以下方式使用 .attr():描述:获取匹配元素集中第一个元素的属性值。
或者
描述:为一组匹配的元素设置一个或多个属性。
your validation should be like:
And that's because your using
.attr()
the wrong way, according to jQuery's documentation: you can use .attr() the following ways:Description: Get the value of an attribute for the first element in the set of matched elements.
Or
Description: Set one or more attributes for the set of matched elements.
请使用以上内容。
please use the above.