无法访问 HTML 中的 JavaScript 函数(main 未定义)



我正在尝试访问HTML中的"main"javascript函数,并得到引用错误说"main"未定义。看不到缺少什么。

<html>
<head>
<title>dataset</title>
<script>
function main(){
....
}
</script>
</head>
<body onload="main()">
<h1>dataset</h1>
</body>
</html>

您的代码工作正常,只需删除"..."从您的函数中添加alert('test)ORconsole.log('test)进行测试

<html>
<head>
<title>websites dataset</title>
<style>
img{
height:100px;
}
</style>
<script>
function main(){
alert('test');
}           
</script>
</head>
<body onload="main()">
<h1>websites dataset</h1>
</body>
</html>

脚本必须在 HTML 正文中定义才能被引用。

因此,只需重新排列您的代码即可:

<html>
<head>
<title>websites dataset</title>
<style>
img{
height:100px;
}
</style>
</head>
<body onload="main()">
<h1>websites dataset</h1>
<script>
function main(){
...
}           
</script>
</body>
</html>

相关内容

最新更新