gpt4 book ai didi

javascript - onLoad 不适用于元素

转载 作者:太空宇宙 更新时间:2023-11-04 14:58:15 25 4
gpt4 key购买 nike

我试图在加载页面上的元素后立即触发操作。我正在使用 Jquery 的 on('load') 功能来执行此操作。

但是它不起作用。这是代码和JSFiddle

console.log("Hi");
$('#complexDiv').on('load', function(){
console.log('Header loaded');
});

HTML

</header>
<body>
<h1>Hello!</h1>
<div id="complexDiv">
<h2>Some text</h2>
<p>A paragraph</p>
<img src="" alt="">
</div>
</body>

最佳答案

我相信加载事件只会在需要外部资源的元素上调用。

来自 jQuery:

The load event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.

.ready() 的案例:

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed and you can use .ready() for that.

对于您的情况,您可以收听窗口加载或更好地使用 ready,因为不推荐使用 load。

$(window).on('load', function(){
console.log('Header loaded');
});

关于javascript - onLoad 不适用于元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38425502/

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