【0035】javascrip脚本放在哪个位置?

有时候,您实际上需要把代码放置在不同地方以符合代码运行要求。比如,您需要在代码全部载入浏览器后再运行代码,那就可以
将<script>标签放在</html>后面去,如果您需要在页面载入的时候运行代码,可以把js代码放入<head></head>之间。

提示:浏览器执行html代码是自上而下的线性过程,<script>作为html代码的一部分同样遵循这个原则!



head 部分中的脚本

<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<title>javascript放在head里面</title>
	<script type=”text/javascript”>
		
	</script>
</head>

<body>
	
</body>
</html>


body 部分中的脚本: 。

(一般放在body最底部)

<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<title>javascript放在head里面</title>
</head>

<body>
	
	<script type=”text/javascript”>
		
	</script>
</body>
</html>

外部js

<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<title>javascript放在head里面</title>
	<script type="text/javascript" src="myScript.js"></script>

</head>

<body>
	
</body>
</html>




测试

<!doctype html>
<html>
<meta http-equiv="content-type" content="text;charset=UTF-8" />
<head>
	<script type="text/javascript">
		document.write("head中代码被执行");
		alert("<head>中代码被执行")
	</script>
</head>
<body>
	body1
	<script type="text/javascript">
		document.write("body1中代码被执行");
		alert("<body>1中代码被执行")
	</script>
	
	body2

	<script type="text/javascript">
		document.write("body2中代码被执行");
		alert("<body>2中代码被执行")
	</script>
</body>
</html>

	<script type="text/javascript">
		document.write("html下代码被执行");
		alert("<html>下代码被执行")
	</script>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值