js script 块的定义顺序的先后导致发生 undefined错误

异常代码:

<!doctype html>

<html>

<head>
</head>

<body>

<script type='text/javascript'>
test();
</script>

<script>
function test(){
alert('test');
}
</script>

</body>
</html>

运行效果:

 

 错误原因:

 

 解决方法:

跳转script 块位置:

 

 建议:将 js function(方法) 放在 <head>里面,这里其他script块就能正常调用。

posted @ 2021-10-25 23:46  温故纳新  阅读(90)  评论(0)    收藏  举报