三种存在位置

一 概念
1、行间式:存在于行间事件中

```html
<body id="body" onload="body.style.backgroundColor='#0ff'">
    
</body>
```

2、内联式:存在于页面script标签中

```html
<body id="body">
    <script type="text/javascript">
		body.style.backgroundColor='#0ff'
	</script>
</body>
```

 3、外联式:存在于外部JS文件,通过script标签src属性链接

```html
index.js文件
body.style.backgroundColor='#0ff'

index.html文件
<script src="./js/index.js"></script>

二 代码示范

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>三种存在位置</title>
</head>
<!-- 1.行间式:js代码块需要出现在事件中 -->
<!-- <body id="body" onload="body.style.backgroundColor='red'"> -->
<!-- <body id="body" onload="alert('hello wolrd')"> -->
<body id="body">
<!-- 2.内联式:js代码块需要出现在script标签中 -->
<script type="text/javascript">
body.style.backgroundColor='orange'
</script>

<!-- 3.外联式:js代码块出现在外部js文件中,需要通过script标签src属性进行链接 -->
<script type="text/javascript" src="./js/01.js"></script>

<!-- 总结 -->
<!-- 三种方式不存在优先级,谁在逻辑下方,谁起作用 -->

</body>
</html>



posted @ 2018-10-11 15:50  不沉之月  阅读(119)  评论(0编辑  收藏  举报