为什么这个要script放在<head></head>间,button放在<body>间才行(代码一)?

为什么这个要script放在<head></head>间,button放在<body>间才行(代码一),都放在<body>之间(代码二)不行吗?

代码一


<html>
<head>
<script type="text/javascript">
function myfunction()
{
var name=prompt("请输入您的名字")
if(name!=null && name!="")
{
document.write("Hello!" +name+ "Nice to meet you!")
}
}
</script>
</head>
<body>
<button onclick="myfunction()">show me your name</button>
</body>
</html>

代码二

<html>

<body>
<button onclick="myfunction()">show me your name</button>

<script type="text/javascript">
function myfunction()
{
var name=prompt("请输入您的名字")
if(name!=null && name!="")
{
document.write("Hello!" +name+ "Nice to meet you!")
}
}
</script>
</body>

</html>


慕九州4302341
浏览 1020回答 1
1回答

qq_从此不再心软_0

如果写在html页面中的,script中的js代码最好是放在body的最底层,因为script标签会阻塞html页面的加载,这样就会影响页面额渲染从而导致用户体验不佳!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript