1
<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
2 < html xmlns = " http://www.w3.org/1999/xhtml " >
3 < head >
4 < meta http - equiv = " Content-Type " content = " text/html; charset=utf-8 " / >
5 < title > 无标题文档 < / title>
6 < script type = " text/javascript " >
7 function doGetElementsByTagName(){
8 var x = document.getElementsByTagName( " input " );
9 alert(x.length);
10 }
11 function doGetElementsByName(){
12 var x = document.getElementsByName( " myInput " );
13 alert(x.length);
14 }
15 function getValue(){
16 var x = document.getElementById( " myHeader " );
17 alert(x.innerHTML);
18 }
19 < / script>
20 < / head>
21
22 < body >
23 < h1 id = " myHeader " onclick = " getValue(); " > 这里是头部 < / h1>
24
25 < p > 点击头部弹出myHeader的内容 < / p>
26 < input type = " text " name = " myInput " size = " 20 " / >
27 < br / >
28 < br / >
29 < input type = " text " name = " myInput " size = " 20 " / >
30 < br / >
31 < br / >
32 < input type = " text " name = " myInput " size = " 20 " / >
33 < br / >
34 < br / >
35 < input type = " button " onclick = " doGetElementsByTagName(); " value = " 有多少个input元素? "/ >
36 < br / >
37 < input type = " button " onclick = " doGetElementsByName(); " value = " 有多少个name的值为myInput? "/ >
38
39 < / body>
40 < / html>
2 < html xmlns = " http://www.w3.org/1999/xhtml " >
3 < head >
4 < meta http - equiv = " Content-Type " content = " text/html; charset=utf-8 " / >
5 < title > 无标题文档 < / title>
6 < script type = " text/javascript " >
7 function doGetElementsByTagName(){
8 var x = document.getElementsByTagName( " input " );
9 alert(x.length);
10 }
11 function doGetElementsByName(){
12 var x = document.getElementsByName( " myInput " );
13 alert(x.length);
14 }
15 function getValue(){
16 var x = document.getElementById( " myHeader " );
17 alert(x.innerHTML);
18 }
19 < / script>
20 < / head>
21
22 < body >
23 < h1 id = " myHeader " onclick = " getValue(); " > 这里是头部 < / h1>
24
25 < p > 点击头部弹出myHeader的内容 < / p>
26 < input type = " text " name = " myInput " size = " 20 " / >
27 < br / >
28 < br / >
29 < input type = " text " name = " myInput " size = " 20 " / >
30 < br / >
31 < br / >
32 < input type = " text " name = " myInput " size = " 20 " / >
33 < br / >
34 < br / >
35 < input type = " button " onclick = " doGetElementsByTagName(); " value = " 有多少个input元素? "/ >
36 < br / >
37 < input type = " button " onclick = " doGetElementsByName(); " value = " 有多少个name的值为myInput? "/ >
38
39 < / body>
40 < / html>
当点击:时,弹出:
当点击:时,弹出:
当点击:时,弹出:
特别是在以下代码中,
1
function
getValue(){
2 var x = document.getElementById( " myHeader " );
3 alert(x.innerHTML);
4 }
2 var x = document.getElementById( " myHeader " );
3 alert(x.innerHTML);
4 }
涉及到了一个知识点:HTML DOM innerHTML ,作用是从对象的起始位置到终止位置的全部内容,包括Html标签