我的脚本在表格中附加了一些代码来加载图像。但是,它会根据表的内容解释图像链接。这有时会导致它加载错误图像。我创建了一个占位符图像,如果第一个图像失败,则会加载该图像,但是如果该占位符无法加载,则脚本将进入无限循环。
$(document).ready(function() {
$('tr[id^="mini"]').each(function(index) {
var s = $(this).find('td:first');
var product = s.text().replace(/\s+/g, '');
s.append('<br><a href="/-p/' + product + '.htm" target="_blank">View Live</a><br><a title="Click for bigger image" href="/PhotoDetails.asp?ShowDESC=Y\&ProductCode=' + product + '" onclick="window.open(\'/PhotoDetails.asp?ShowDESC=Y\&ProductCode=' + product + '\',\'Option\',\'scrollbars=yes,menubar=no,status=no,location=no,width=600,height=640\'); return false;" target="_blank"><img src="/v/vspfiles/photos/' + product + '-0.jpg" border="0" style="max-width:150px;max-height:150px" id="monkey-inserted-image" /></a>');
});
// This checks for images that do not load //
$('img#monkey-inserted-image').error(function() {
$(this).removeAttr("id").attr("src", "/tamper/noimage1.png");
// If this image fails to load, an infinite loop is created //
});
});
我尝试使用 .removeAttr() 希望这会停止循环,但事实并非如此。