我想知道如何確定圖像是否在指定路徑上可用。我想,如果有指定的路徑上沒有圖像顯示錯誤消息...如何確定指定路徑上沒有圖像
對於如:
<img src="images/products/default.bmp" alt="NoImage"/>
但如果這條道路上沒有任何圖像default.bmp然後按Alt信息noImage會顯示,但除此之外,我還想顯示錯誤消息...爲此,我需要確定沒有圖像,我怎麼能。請回復
感謝 羅米
我想知道如何確定圖像是否在指定路徑上可用。我想,如果有指定的路徑上沒有圖像顯示錯誤消息...如何確定指定路徑上沒有圖像
對於如:
<img src="images/products/default.bmp" alt="NoImage"/>
但如果這條道路上沒有任何圖像default.bmp然後按Alt信息noImage會顯示,但除此之外,我還想顯示錯誤消息...爲此,我需要確定沒有圖像,我怎麼能。請回復
感謝 羅米
使用jQuery
$('img').error(function(){
console.log('error');
});
另一種方式
var img = document.getElementsByTagName('img')[0];
if(img.complete){
console.log('loaded');
}else{
console.log('not loaded');
}
使用IMG 「的onerror」 事件,例如
<img id="image" alt='img' src="myImg.gif"/>
<script>
function noImg() {
alert("Can't find image!");
}
document.getElementById("image").onerror = noImg;
</script>
如果無法找到或加載img src,它會被觸發。
您還可以查看圖像的寬度,如果是0:
if ($(‘#imageID’).getwidth() == 0) {
alert("Can't find image!");
}
我第二本。 PLZ回答 – alt 2011-06-13 05:53:45
[jQuery/Javascript替換破碎的圖像]可能的重複(http://stackoverflow.com/questions/92720/jquery-javascript-to-replace-broken-images) – mplungjan 2011-06-13 06:04:08