gpt4 book ai didi

javascript - 是否可以将 'error' 事件的事件监听器添加到 HTMLImageElement?

转载 作者:行者123 更新时间:2023-11-28 06:15:40 25 4
gpt4 key购买 nike

我需要为所有图像添加“错误”事件监听器。但应该使用 window.HTMLImageElement 添加(或通过 window.HTMLImageElement.constructor、window.HTMLImageElement.prototype.constructor 等)

最佳答案

不幸的是,the error event doesn't bubble ,因此您不能使用委托(delegate)处理程序。从该链接(第 14 步的最后一个“否则”):

Either the image data is corrupted in some fatal way such that the image dimensions cannot be obtained, or the image data is not in a supported file format; the user agent must set the img element to the broken state, abort the fetching algorithm, discarding any pending tasks generated by that algorithm, and then queue a task to first fire a simple event named error at the img element and then fire a simple event named loadend at the img element.

其中“触发一个简单事件”说:

Firing a simple event named e means that a trusted event with the name e, which does not bubble (except where otherwise stated) and is not cancelable (except where otherwise stated), and which uses the Event interface, must be created and dispatched at the given target.

(我的重点)

相反,您必须将处理程序挂接到页面上的每个图像(您可以使用 getElementsByTagNamequerySelectorAll 查找它们),并仔细检查图像是否已经完成(在这种情况下,事件可能已经被触发)。

关于javascript - 是否可以将 'error' 事件的事件监听器添加到 HTMLImageElement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35990638/

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