JavaScript續

DOM:一種方便處理層次型文檔的一種技術。html

獲取HTML元素: java

一、getElementById 方法:數組

  根據HTML元素的ID屬性來獲得HTML元素。在HTML文檔中,id屬性值是惟一的。  spa

 

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

二、getElementsByTagName方法 :code

  能夠根據HTML的標籤類型來得到一個相同的HTML元素的數組。 htm

 

三、getElementsByName 方法: 對象

  經過HTML元素的name屬性得到相應的HTML元素集合。因爲HTML元素的name屬於並不惟一,所以,使用getElementsByName方法有可能獲得多個blog

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

複製代碼
<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>
複製代碼
相關文章
相關標籤/搜索