forked from liang996/Javascript-Learning-notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
js使用.txt
192 lines (98 loc) · 2.82 KB
/
js使用.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
1.<script> 标签
在 HTML 中,JavaScript 代码必须位于 <script> 与 </script> 标签之间。
<!DOCTYPE html>
<html>
<body>
<h2>Body 中的 JavaScript</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML="插入内容"
</script>
</body>
</html>
2.JavaScript 函数和事件
JavaScript 函数是一种 JavaScript 代码块,它可以在调用时被执行。
例如,当发生事件时调用函数,比如当用户点击按钮时。
注意:旧的JavaScript例子也许会用到type属性:<script type="text/javascript">
注释:type 属性不是必需的。JavaScript 是 HTML 中的默认脚本语言。
3.<head> 或 <body> 中的 JavaScript
您能够在 HTML 文档中放置任意数量的脚本。
脚本可被放置与 HTML 页面的 <body> 或 <head> 部分中,或兼而有之。
4.<head> 中的 JavaScript
在本例中,JavaScript 函数被放置于 HTML 页面的 <head> 部分。
该函数会在按钮被点击时调用:
<!DOCTYPE html>
<html>
<head>
<script>
function change(){
document.getElementById("demo").innerHTML="哈哈哈"
}
</script>
</head>
<body>
<P id="demo" >你好<P>
<button onclick="change()">改变</bttton>
</body>
</html>
5.<body> 中的 JavaScript
在本例中,JavaScript 函数被放置于 HTML 页面的 <body> 部分。
该函数会在按钮被点击时调用:
实例
<!DOCTYPE html>
<html>
<body>
<h1>A Web Page</h1>
<p id="demo">一个段落</p>
<button type="button" onclick="myFunction()">试一试</button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "段落被更改。";
}
</script>
</body>
</html>
6.外部脚本
脚本可放置与外部文件中:
外部文件:myScript.js
function myFunction() {
document.getElementById("demo").innerHTML = "段落被更改。";
}
外部脚本很实用,如果相同的脚本被用于许多不同的网页。
JavaScript 文件的文件扩展名是 .js。
如需使用外部脚本,请在 <script> 标签的 src (source) 属性中设置脚本的名称:
<!DOCTYPE html>
<html>
<body>
<h2>外部 JavaScript</h2>
<p id="demo">一个段落。</p>
<button type="button" onclick="myFunction()">试一试</button>
<p>(myFunction 存储在名为 "myScript.js" 的外部文件中。)</p>
<script src="/demo/myScript.js"></script>
</body>
</html>
您可以在 <head> 或 <body> 中放置外部脚本引用。
该脚本的表现与它被置于 <script> 标签中是一样的。
注释:外部脚本不能包含 <script> 标签。
7.外部 JavaScript 的优势
在外部文件中放置脚本有如下优势:
?分离了 HTML 和代码
?使 HTML 和 JavaScript 更易于阅读和维护
?已缓存的 JavaScript 文件可加速页面加载
如需向一张页面添加多个脚本文件 - 请使用多个 script 标签:
实例
<script src="myScript1.js"></script>
<script src="myScript2.js"></script>
8.外部引用
可通过完整的 URL 或相对于当前网页的路径引用外部脚本:
本例使用完整的 URL 来链接至脚本:
<!DOCTYPE html>
<html>
<body>
<h2>外部 JavaScript</h2>
<p id="demo">一个段落。</p>
<button type="button" onclick="myFunction()">试一试</button>
<p>(myFunction 存储在名为 "myScript.js" 的外部文件中。)</p>
<script src="="https://www.w3school.com.cn/js/demo/myScript.js"></script>
</body>
</html>