-
Notifications
You must be signed in to change notification settings - Fork 498
/
5.HTML中怎么写JS.html
45 lines (41 loc) · 1.33 KB
/
5.HTML中怎么写JS.html
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
<!doctype html>
<html >
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script>
//嵌入式的写法,直接放在上面,获取不到ID;
/*var oDiv3=document.getElementById("btn2");
console.log(oDiv3);//null*/
window.onload=function () {
var oDiv3=document.getElementById("btn2");
console.log(oDiv3,"window.onload");//null
}
</script>
</head>
<body>
<!--JS行内写法:JS直接写在标签上-->
<div>这是一段信息;<a href="javascript:;">这是百度的连接</a></div>
<input type="button" value="click me 1" onclick="alert('这是行内的写法')">
<br>
<!--JS嵌入式的写法 :JS写在script标签内-->
<input type="button" value="click me 2" id="btn1">
<br>
<br>
<br>
<input type="button" value="click me 3" id="btn2">
<br>
<img src="https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png" alt="">
<script>
//嵌入式的写法
var oDiv=document.getElementById("btn1");
oDiv.onclick=function () {
alert("这是嵌入式的写法")
}
</script>
<script src="src/btn.js"></script>
</body>
</html>