我试图让 onError 事件工作,但到目前为止它只在 Internet Explorer 9 中工作。我尝试了几个代码,但基本上归结为:
<img class="a_toc_image" src="' + asap.imgurl + '" onError="this.src=\'images/no_image.png\';" />
- 互联网浏览器 9:
- 成功:从我们的数据库中获取图像
失败:显示 no_image.png
铬 20.0.11:
- 成功:从我们的数据库中获取图像
失败:只是空格
火狐 14.0.1:
- 成功:从我们的数据库中获取图像
- 失败:损坏的图像图标
此代码上的许多主要是美学变体(例如省略或放入 " 或 ' )会产生类似的结果。这个特定的变体在 Iexplorer 中产生了堆栈溢出,但除此之外没有任何改变:
<img class="a_toc_image" src="' + asap.imgurl + '" onError=this.src="\images/no_image.png()" />
谁能告诉我这里出了什么问题以及为什么它只能在 Iexplorer 9 中工作?
谢谢!
-添加:
在无法加载的图像上使用 Chrome 中的“检查元素”时,我看到:
<img class="a_toc_image" src="images/no_image.png" onerror="this.src='images/no_image.png';">
所以看起来它正在生成正确的输出,但由于某种原因不会显示图像,对吗?我刚才试图放一个 .jpg 而不是 .png (也许 Chrome 不会显示 .png 图像),但这也不能解决任何问题。
-加法2,前面的代码
// General functions
var open_mask = function () {
//Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(window).width();
//Set height and width to mask to fill up the whole screen
$('#mask').css({'width':maskWidth,'height':maskHeight});
//transition effect
$('#mask').fadeIn(1000);
$('#mask').fadeTo("fast",0.7);
};
var center_dialog = function (dialog) {
//Get the window height and width
var winH = $(window).height();
var winW = $(window).width();
var dialog_top = (winH/2-dialog.height()/2) + $(window).scrollTop();
var dialog_left = (winW/2-dialog.width()/2) + $(window).scrollLeft();
dialog_top = (dialog_top >= 0) ? dialog_top : 0;
dialog_left = (dialog_left >= 0) ? dialog_left : 0;
dialog.css('top', dialog_top);
dialog.css('left', dialog_left);
};
//function that creates posts
var updatepostHandler = function(postsJSON) {
$.each(postsJSON,function(i,asap) {
if(i === 0) {
first = asap.first;
last = asap.last;
} else {
if(asap.type === 'article') {
$('<div></div>') //create the HTML
.addClass('solid')
.html('<div class="titbox">' + asap.title + '</div> \
<div class="boxinsolid"> \
<div class="aubox">' + asap.authors + '</div> \
<div class="doibox"> DOI: ' + asap.doi + ' </div> \
<div class="joubox">' + asap.journal + '</div> \
</div> \
<div class="imgbox"> \
<img class="a_toc_image" src="' + asap.imgurl + '" onError="this.src=\'images/no_image.png\';" /> \
</div>')
.click(function(e) {
open_details(e, asap.id);
})
.prependTo($('#container'))
.slideDown('slow')
} else if(asap.type === 'ad') {
$('<div></div>') //create the HTML
.addClass('ad')
.html('<div class="titbox">' + asap.title + '</div> \
<div class="boxinsolid"> \
<div class="aubox">' + asap.authors + '</div> \
<div class="doibox"> </div> \
<div class="joubox">' + asap.company + '</div> \
</div> \
<div class="imgbox"> \
<img class="a_toc_image" src="' + asap.image + '" onError="this.src=\'images/no_image.png\';" /> \
</div>')
.click(function(e) {
open_details(e, asap.ad_id);
})
.prependTo($('#container'))
.slideDown('slow')
}
};
});
};