解決js圖片加載時出現(xiàn)404的問題
運(yùn)營網(wǎng)站久了之后,無法避免會出現(xiàn)圖片404的情況,原因可能是圖片文件本來就不存在或目前不存在。常見的解決方案是將404圖片隱藏或者是替換為默認(rèn)的圖片。
img標(biāo)簽事件屬性
img標(biāo)簽可使用的時間屬性有:
onabort, onbeforeunload, onblur, onchange, onclick, oncontextmenu, ondblclick, ondrag, ondragend, ondragenter, ondragleave, ondragover, ondragstart, ondrop, onerror, onfocus, onkeydown, onkeypress, onkeyup, onload, onmessage, onmousedown, onmousemove, onmouseover, onmouseout, onmouseup, onmousewheel, onresize, onscroll, onselect, onsubmit, onunload
img標(biāo)簽常用的事件如下:
onerror:圖像加載過程中發(fā)生錯誤時被觸發(fā)。
onabort:圖片加載的時候,用戶通過點(diǎn)擊停止加載時觸發(fā),通常在這里觸發(fā)一個提示:“圖片正在加載”。
onload:當(dāng)圖片加載完成之后觸發(fā)。
1. 對圖片監(jiān)聽onerror事件
<img src="someimage.png" onerror="imgError(this);" />
// 原生JS:
function imgError(image){
// 隱藏圖片
image.style.display = 'none';
// 替換為默認(rèn)圖片
// document.getElementById("img").setAttribute("src", "images/demo.png");
}
// 使用jQuery處理:
function imgError(image){
$(image).hide();
// $(this).attr("src", "images/demo.png");
}
注意:需要將處理函數(shù)定義在head,防止圖片加載出錯時沒有讀取到處理函數(shù)
2. 使用jQuery監(jiān)聽error
// 通常不會再HTML里面內(nèi)聯(lián)js,可以使用.error對圖片進(jìn)行監(jiān)聽處理
$('#test img').error(function() {
$(this).hide();
// $(this).attr("src", "images/demo.png");
});
注意:jQuery加載需要在img前,處理函數(shù)需在img后
3. 使用函數(shù)處理
// 原生JS解決方案
function $id(id) {
return !id || id.nodeType === 1 ? id : document.getElementById(id);
}
function isType(o, t) {
return (typeof o).indexOf(t.charAt(0).toLowerCase()) === 0;
}
// 主要邏輯
function image(src, cfg) {
var img, prop, target;
cfg = cfg || (isType(src, 'o') ? src : {});
img = $id(src);
if (img) {
src = cfg.src || img.src;
} else {
img = document.createElement('img');
src = src || cfg.src;
}
if (!src) {
return null;
}
prop = isType(img.naturalWidth,'u') ? 'width' : 'naturalWidth';
img.alt = cfg.alt || img.alt;
// Add the image and insert if requested (must be on DOM to load or
// pull from cache)
img.src = src;
target = $id(cfg.target);
if (target) {
target.insertBefore(img, $id(cfg.insertBefore) || null);
}
// Loaded?
if (img.complete) {
if (img[prop]) {
if (isType(cfg.success,'f')) {
cfg.success.call(img);
}
} else {
if (isType(cfg.failure,'f')) {
cfg.failure.call(img);
}
}
} else {
if (isType(cfg.success,'f')) {
img.onload = cfg.success;
}
if (isType(cfg.failure,'f')) {
img.onerror = cfg.failure;
}
}
return img;
}
以上函數(shù)有許多用處:
1. 獲取圖片信息:圖片是否可下載,圖片寬高
image('img',{
success : function () { alert(this.width + "-" + this.height); },
failure : function () { alert('image 404!'); },
});
// 驗證資源是否下載
image('images/banner/banner_2.jpg', {
success : function () {console.log('sucess')},
failure : function () {console.log('failure')},
target : 'myContainerId',
insertBefore : 'someChildOfmyContainerId'
});
2. 下載并插入圖片
var report = $id('report'),
callback = {
success : function () {
report.innerHTML += '<p>Success - ' + this.src + ' ('+this.offsetWidth+'x'+this.offsetHeight+')</p>';
},
failure : function () {
report.innerHTML += '<p>Failure - ' + this.src + ' ('+this.offsetWidth+'x'+this.offsetHeight+')</p>';
},
target : 'target'
};
image('img', callback);
image('images/banner/banner_2.jpg', callback);
以上就是js針對圖片加載時出現(xiàn)404問題的解決辦法,希望大家有所收獲。
- JS實(shí)現(xiàn)圖片預(yù)加載無需等待
- Jquery.LazyLoad.js修正版下載,實(shí)現(xiàn)圖片延遲加載插件
- JS判斷圖片是否加載完成方法匯總(最新版)
- javascript實(shí)現(xiàn)圖片延遲加載方法匯總(三種方法)
- jquery插件lazyload.js延遲加載圖片的使用方法
- js實(shí)現(xiàn)圖片在未加載完成前顯示加載中字樣
- 關(guān)于JS判斷圖片是否加載完成且獲取圖片寬度的方法
- JavaScript判斷圖片是否已經(jīng)加載完畢的方法匯總
- js或者jquery判斷圖片是否加載完成實(shí)現(xiàn)代碼
- JavaScript canvas實(shí)現(xiàn)加載圖片
相關(guān)文章
Javascript BOM學(xué)習(xí)小結(jié)(六)
BOM:BrowserObjectModel,瀏覽器對象模型,提供JS中對瀏覽器的各種操作的對象,是JS應(yīng)用中唯一沒有相關(guān)標(biāo)準(zhǔn)的部分,這事BOM經(jīng)常出現(xiàn)問題的所在,主要用于處理瀏覽器窗口與框架,瀏覽器特有的JS擴(kuò)展也被默認(rèn)為BOM的一部分,而各瀏覽器之間的公有對象就成了默認(rèn)的標(biāo)準(zhǔn)2015-11-11
JavaScript 開發(fā)工具webstrom使用指南
本文給大家推薦了一款非常熱門的javascript開發(fā)工具webstrom,著重介紹了webstrom的特色功能、設(shè)置技巧、使用心得以及快捷鍵匯總,非常的全面。2014-12-12
Javascript實(shí)現(xiàn)圖片輪播效果(二)圖片序列節(jié)點(diǎn)的控制實(shí)現(xiàn)
這篇文章主要介紹了Javascript實(shí)現(xiàn)圖片輪播效果(二)圖片序列節(jié)點(diǎn)的控制實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2016-02-02
BootStrap Typeahead自動補(bǔ)全插件實(shí)例代碼
本文給大家介紹BootStrap Typeahead自動補(bǔ)全插件的實(shí)例代碼,非常不錯,具有參考借鑒價值,需要的朋友參考下2016-08-08

