完美相容多瀏覽器的js判斷圖片路徑代碼匯總,js匯總

來源:互聯網
上載者:User

完美相容多瀏覽器的js判斷圖片路徑代碼匯總,js匯總

第一種方式

//檢查圖片是否存在function CheckImgExists(imgurl) {var ImgObj = new Image(); //判斷圖片是否存在ImgObj.src = imgurl;//沒有圖片,則返回-1if (ImgObj.fileSize > 0 || (ImgObj.width > 0 && ImgObj.height > 0)) {alert(imgurl + '存在');} else {alert(imgurl + '不存在');}}CheckImgExists('/editor/simple/FileIconDir/rar.gif');CheckImgExists('/editor/simple/FileIconDir/rarsdfs.gif');

另外一種實現方式

複製代碼 代碼如下:<img src="原圖片" alt="圖片" onerror="this.src='替換圖片'">

第三種方式

<script language="javascript" type="text/javascript">//載入失敗調用此函數function defaultImg(T){T.src = "images/closelabel.gif";//預設圖片地址}//載入成功後會調用此函數function LoadOK(){alert("LoadOK");}</script></head><body><img src="123.jpg" onerror="defaultImg(this);" onload="LoadOK()"/>  <!--123.jpg 是錯誤圖片地址-->

以上所述就是本文的全部內容了,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.