body onload 和 window.onload 同时进行

发布于 2025-01-04 18:14:54 字数 223 浏览 0 评论 0原文

我可以同时使用body onload和window.onload吗?我已经尝试使用此代码

<body onload = "alertFirst()">
</body>
<script>
    window.onload = alertSec;
</script>

但它不起作用。我只是需要有人向我确认一下。非常感谢

Can I use body onload and window.onload at the same time? I've tried it using this code

<body onload = "alertFirst()">
</body>
<script>
    window.onload = alertSec;
</script>

But it didn't work. I just need someone to confirm it to me. Many thanks

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

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

发布评论

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

评论(4

猥︴琐丶欲为 2025-01-11 18:14:55

你的问题的答案是“不”。然而,有一些方法可以解决这个问题。

将两个调用添加到一个 onload 函数是理想的选择,但如果您/必须/在添加一个 onload 处理程序后添加一个 onload 处理程序,并且您没有使用有助于此操作的框架,则可以像这样完成:

<html>
    <head>
        <script type="text/javascript">
            function alertFirst(){
                alert('First');
            }
            function alertSec(){
                alert('Second');
            }
        </script>
    </head>
    <body onload="alertFirst();">
        content
    </body>
    <script>
        var func = document.body.onload;
        window.onload=function(){
            func();
            alertSec();
        }
    </script>
</html>

The answer to your question is "no". However there are ways around it.

Adding both calls to one onload function is ideal, but if you /have/ to add an onload handler after one is already added, and you are not using a framework which facilitates this, you can get by like this:

<html>
    <head>
        <script type="text/javascript">
            function alertFirst(){
                alert('First');
            }
            function alertSec(){
                alert('Second');
            }
        </script>
    </head>
    <body onload="alertFirst();">
        content
    </body>
    <script>
        var func = document.body.onload;
        window.onload=function(){
            func();
            alertSec();
        }
    </script>
</html>
撕心裂肺的伤痛 2025-01-11 18:14:55

您可以(通过添加事件处理程序),但您不应该同时拥有两者

而是添加对 window.onload 的调用:

<html>
<head>
<script>
  window.onload = function() {
    alertFirst();
    alertSec();
  }
</script>
</head>

<body>
</body>

You can (by adding event handler(s)) but you should NOT have both

Instead add the call to the window.onload:

<html>
<head>
<script>
  window.onload = function() {
    alertFirst();
    alertSec();
  }
</script>
</head>

<body>
</body>
忆悲凉 2025-01-11 18:14:55

不,document.body.onload 实际上映射到 window.onload。您可以自行检查 - 当您有 console.log(window.onload), a( ) 被打印到控制台中。

您可以做的是让一个 onload 事件处理程序调用另外两个函数。

window.onload = function () {
  a();
  b();
};

或两个事件监听器

window.addEventListener('load', a, false);
window.addEventListener('load', b, false);

No, document.body.onload is actually mapped to window.onload. You can check yourself—when you have <body onload="a()"> and to console.log(window.onload), a() is printed out into the console.

What you can do is to have one onload event handler that calls two other functions.

window.onload = function () {
  a();
  b();
};

or two event listeners

window.addEventListener('load', a, false);
window.addEventListener('load', b, false);
南街九尾狐 2025-01-11 18:14:55

如果您要使用的一个或多个脚本在 BODY HTML 标记中具有事件处理程序,您仍然可以将其移至 javascript 代码中。请参阅下面的示例:

脚本#1:

<script language="javascript">

function start(){
...code for script #1...
}

</script>

<body onload="start()">

脚本#2:

<script language="javascript">

function init(){
...code for script #2...
}

window.onload=init;

</script>

结果:

<script language="javascript">

function start(){
...code for script #1...
}

function init(){
...code for script #2...
}

window.onload=function(){
start();
init();
}

</script>

<body>

我认为它可能会对您有所帮助。

If one or more of the scripts you want to use has the event handler in the BODY HTML tag, you can still move it to into javascript code. See the example below:

Script #1:

<script language="javascript">

function start(){
...code for script #1...
}

</script>

<body onload="start()">

Script #2:

<script language="javascript">

function init(){
...code for script #2...
}

window.onload=init;

</script>

Result:

<script language="javascript">

function start(){
...code for script #1...
}

function init(){
...code for script #2...
}

window.onload=function(){
start();
init();
}

</script>

<body>

I think it may can help you.

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