文档'负载'事件回调未调用;我怎么弄乱了?

发布于 2025-01-19 08:49:10 字数 1095 浏览 6 评论 0 原文

在此HTML文档中,在Chrome中,我的负载事件回调都没有被调用:

<!DOCTYPE html>
<html>
    <head>
        <title>test</title>
        <script>
            console.log('just checking');
            function someFunction () { console.log('test 3'); }
            document.addEventListener('load', () => console.log('test 1'));
            document.addEventListener('load', function () { console.log('test 2'); });
            document.addEventListener('load', someFunction);
        </script>
    </head>
    <body>
    </body>
</html>

但是,我可以看到它们是在检查员中设置的:

”在此处输入图像描述”

,控制台中没有错误。

我几乎可以肯定,这对我来说是一个琐碎的错误,我无法弄清楚它是什么。

出于原因,我花了大量时间在互联网上搜索,但是在大多数情况下,我发现的有关失败负载回调的每篇文章通常与在准备就绪之前访问DOM有关,这并不适用。

我手动地尝试在脚本上设置 defer 属性,但没有效果。

我在这里想念什么...?

In this HTML document, in Chrome, none of my load event callbacks are called:

<!DOCTYPE html>
<html>
    <head>
        <title>test</title>
        <script>
            console.log('just checking');
            function someFunction () { console.log('test 3'); }
            document.addEventListener('load', () => console.log('test 1'));
            document.addEventListener('load', function () { console.log('test 2'); });
            document.addEventListener('load', someFunction);
        </script>
    </head>
    <body>
    </body>
</html>

However, I can see that they are set in the inspector:

enter image description here

And there are no errors in the console.

I am almost certain this is some trivial error on my part, and I can't figure out what it is.

I spent a fair amount of time searching the internet for reasons, but for the most part every post I found about failed load callbacks generally had to do with accessing the DOM before it was ready, which doesn't really apply here.

I hand-wavily tried setting the defer attribute on the script but it had no effect.

What am I missing here... ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港巷口少年归 2025-01-26 08:49:11
<!DOCTYPE html>
<html>
<head>
    <title>test</title>
    <script>
    function docReady(func) {
        document.addEventListener("DOMContentLoaded", function (event) {
           func(event);
        });
    }
        function someFunction () { console.log('test 3'); }
        docReady(() => console.log('test 1'));
        docReady(function () { console.log('test 
 2'); });
        docReady(someFunction);
    </script>
</head>
<body>
</body>
</html>

使用“ domcontentloaded”而不是“加载”

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
    <script>
    function docReady(func) {
        document.addEventListener("DOMContentLoaded", function (event) {
           func(event);
        });
    }
        function someFunction () { console.log('test 3'); }
        docReady(() => console.log('test 1'));
        docReady(function () { console.log('test 
 2'); });
        docReady(someFunction);
    </script>
</head>
<body>
</body>
</html>

use 'DOMContentLoaded' instead 'load'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文