javascript 简单的 onload 事件没有触发?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15469804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple onload event not firing?
提问by user2127833
Just made a basic program to fin the root of my problem and I can't seem to find why this is not working at all.
刚刚制作了一个基本程序来解决我的问题的根源,我似乎无法找到为什么这根本不起作用。
HTML
HTML
<html lang="en">
<head>
<script type="text/javascript" src="carjava.js"></script>
</head>
<body onload="hi()">
hi
</body>
</html>
JS
JS
function hi() {
alert("hi");
}
I want an alert to pop up when the page loads but nothing happens, any ideas?
我想在页面加载时弹出警报但没有任何反应,有什么想法吗?
回答by Danny Beckett
I can't tell you why it isn't firing, but this should work:
我不能告诉你为什么它没有触发,但这应该有效:
HTML
HTML
<html lang="en">
<head>
<script type="text/javascript" src="carjava.js"></script>
</head>
<body>
hi
</body>
</html>
JS
JS
function hi() {
alert("hi");
}
window.onload = hi;
回答by Falaque
following code is working fine for me:
以下代码对我来说工作正常:
<html lang="en">
<head>
<script type="text/javascript" src="carjava.js"></script>
<script type="text/javascript">
function hi()
{
alert('hi');
}
</script>
</head>
<body onload="hi()">
hi
</body>
</html>
Its working fine in Firefox & Chrome. Must be something with js file.
它在 Firefox 和 Chrome 中运行良好。必须是 js 文件的东西。