gpt4 book ai didi

javascript - 将 404 上的图像替换为标准图像

转载 作者:行者123 更新时间:2023-12-01 01:47:41 25 4
gpt4 key购买 nike

我试图循环一组具有相同类的图像并将错误处理程序附加到它们,以便在错误时我通过 $(this).attr('src' 将图像替换为标准图像, 'img path'); 我将此 .each() 放在 document.ready() 的末尾。循环正确迭代,但是 .error() 没有触发,尽管我有 404 错误。我尝试了 .click() 和警报来查看会发生什么,然后警报弹出了。我该如何解决这个问题,以便 .error() 被解雇?

$('img.thumbnail-img').each(function(index, element)
{
$(element).error(function()
{
load_thumbnails_error('img', this);
});
});

最佳答案

来自jQuery documentation :

The event handler must be attached before the browser fires the error event, which is why the example sets the src attribute after attaching the handler. Also, the error event may not be correctly fired when the page is served locally; error relies on HTTP status codes and will generally not be triggered if the URL uses the file: protocol.

如果我理解这一点,这意味着如果您在 $(document).ready 函数中执行 .each 循环,则所有图像都已触发错误。

关于javascript - 将 404 上的图像替换为标准图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18458258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com