如何监听要加载的特定元素?
是否可以监听要加载的特定 DOM 元素?我有一个在整个页面加载时调用的 window.onload 事件,但我正在寻找一种方法来为加载的每个 DOM 元素调用它。 (onload
不会冒泡吗?)
示例:
function init() {
alert(this.id); // Should be in context of DOM element that bubbled up when loaded
}
window.onload = init;
<div id="one">One</div>
<div id="two">One</div>
<div id="three">One</div>
我希望这段代码能够提醒一、二、三。
Is it possible to listen for a specific DOM element to be loaded? I have a window.onload
event that is called when the whole page loads, but I'm looking for a way for it to be called for each DOM element that gets loaded. (Does onload
not bubble up?)
Example:
function init() {
alert(this.id); // Should be in context of DOM element that bubbled up when loaded
}
window.onload = init;
<div id="one">One</div>
<div id="two">One</div>
<div id="three">One</div>
I want this code to alert one, two, three.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
onload
事件不会像这样冒泡,这里有 2 个选项:onload
events don't bubble like this, you have 2 options here: