body onload 和 window.onload 同时进行
我可以同时使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的问题的答案是“不”。然而,有一些方法可以解决这个问题。
将两个调用添加到一个 onload 函数是理想的选择,但如果您/必须/在添加一个 onload 处理程序后添加一个 onload 处理程序,并且您没有使用有助于此操作的框架,则可以像这样完成:
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:
您可以(通过添加事件处理程序),但您不应该同时拥有两者
而是添加对 window.onload 的调用:
You can (by adding event handler(s)) but you should NOT have both
Instead add the call to the window.onload:
不,
document.body.onload
实际上映射到window.onload
。您可以自行检查 - 当您有和
console.log(window.onload)
,a( )
被打印到控制台中。您可以做的是让一个 onload 事件处理程序调用另外两个函数。
或两个事件监听器
No,
document.body.onload
is actually mapped towindow.onload
. You can check yourself—when you have<body onload="a()">
and toconsole.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.
or two event listeners
如果您要使用的一个或多个脚本在 BODY HTML 标记中具有事件处理程序,您仍然可以将其移至 javascript 代码中。请参阅下面的示例:
脚本#1:
脚本#2:
结果:
我认为它可能会对您有所帮助。
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 #2:
Result:
I think it may can help you.