本文介绍了主体onload和window.onload同时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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:

脚本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>

我认为它可以为您提供帮助.

I think it may can help you.

这篇关于主体onload和window.onload同时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 15:11