JavaScript续

DOM:一种方便处理层次型文档的一种技术。

获取HTML元素: 

1、getElementById 方法:

  根据HTML元素的ID属性来得到HTML元素。在HTML文档中,id属性值是唯一的。  

 

  <body>
<!-- 用于输入消息文本框 -->
<input type = "text" id ="name"/>
</body>

2、getElementsByTagName方法 :

  可以根据HTML的标签类型来获得一个相同的HTML元素的数组。 

 

3、getElementsByName 方法: 

  通过HTML元素的name属性获得相应的HTML元素集合。由于HTML元素的name属于并不唯一,因此,使用getElementsByName方法有可能得到多个

相同的name属性值的HTML元素。如:

复制代码
<html>
<head>
<title>getElementHtml</title>
<script type = "text/javaScript">
function volution()
{
//获得一个文本框对象数组
       var oTexts = document.getElementsByName("tests") ;
if (oTexts.length == 0 )
{
alert("Error!");
return ;
}
alert(oTexts.length) ;
for (var i = 0 ; i < oTexts.length ; ++ i)
oTexts[i].value = i ;
}
</script>
</head>
<body>
<!-- 5个文本框 -->
<input type = "text" name = "tests" /> <p/>
<input type = "text" name = "tests" /> <p/>
<input type = "text" name = "tests" /> <p/>
<input type = "text" name = "tests" /> <p/>
<input type = "text" name = "tests" /> <p/>
<input type = "button" value = "getElementByName" onclick = "volution()" />
</body>
</html>
复制代码
 
posted @ 2014-06-21 13:42  AinRain  阅读(255)  评论(0编辑  收藏  举报