-
[代碼]img標簽src對應的圖片不存在,顯示一個默認的
onerror="this.src='img/default.jpg'"

- [代碼][CSS]引用(推薦夠用了)
<script type="text/javascript">
t = document.getElementsByClassName("defaultImg");
for(i = 0; i < t.length; i++){
t.item(i).onerror = function(){
this.src = "test.gif"
}
}
</script>
//采用 className 是為了更好的適應頁面,不是每個img我們都要這樣做的,甚至不同地方的img
//我們要顯示不同的默認圖片。(采用img同樣做法)。
//采用id則是犯了錯誤,id 導致只會拿到第一個id相符的。
//此代碼經過測試在 非IE 的browser上正常工作。
//為了兼容IE個廢物,請使用以下代碼:
<script type="text/javascript">
t = document.getElementsByTagName("img");
for(i = 0; i < t.length; i++){
t.item(i).onerror = function(){
if(this.id =="defaultImg"){
this.src = "test.gif";
this.onerror = null;
}
}
}
</script>