javascript 如何正確使用getElementById,getElementsByName(), and getElementsByTagName()

來源:互聯網
上載者:User
 

WEB標準下可以通過getElementById(), getElementsByName(), and getElementsByTagName()訪問DOCUMENT中的任一個標籤。

(1)getElementById():

                      getElementById()可以訪問DOCUMENT中的某一特定元素,顧名思義,就是通過ID來取得元素,所以只能訪問設定了ID的元素。

(2)getElementsByName():

                     該方法是通過NAME屬性來獲得元素,但注意區別:getElementById()中是element,而getElementsByName()是elements。顯而易見,getElementsByName()傳回值有很多,這是因為DOCUMENT中每一個元素的ID是唯一的,但NAME卻可以重複。如果一個文檔中有兩個以上的標籤NAME相同,那麼getElementsByName()就可以取得這些元素組成一個數組。

例如:

<div name="docname" id="docid1"></div>
<div name="docname" id="docid2"></div>
那麼可以用getElementsByName("docname")獲得這兩個DIV元素,用getElementsByName("docname")[0]訪問第一個DIV,用getElementsByName("docname")[1]訪問第二個DIV。

(3)getElementsByTagName()
                      getElementsByTagName()是通過TAGNAME(標籤名稱)來獲得元素,一個DOCUMENT中當然會有相同的標籤,所以這個方法也是取得一個數組。

例如:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Byname,tag</title>
<style type="text/css">
<!--
#docid1,#docid2{
margin:10px;
height:400px;
width:400px;
background-color:#999;}
-->
</style>
</head>
<body>
<div name="docname" id="docid1" onClick="bgcolor()"></div>
<div name="docname" id="docid2" onClick="bgcolor()"></div>
</body>
</html>
<script language="JavaScript" type="text/JavaScript">
<!--
function bgcolor(){
var docnObj=document.getElementsByTagName("div");
docnObj[0].style.backgroundColor = "black";
docnObj[1].style.backgroundColor = "black";
}
-->
</script>

總結一下標準DOM,訪問某一特定元素盡量用標準的getElementById(),訪問標籤用標準的getElementByTagName(),但IE不支援getElementsByName(),所以就要避免使用getElementsByName(),但getElementsByName()和不符合標準的document.all[]也不是全無是處,它們有自己的方便之處,用不用那就看網站的使用者使用什麼瀏覽器,由你自己決定了。

  Javascript中的getElementById十分常用,但在標準的頁面中,一個id只能出現一次,如果我想同時控制多個元素,例如點一個連結, 讓多個層隱藏,該怎麼做?用class,當然,同一個class是可以允許在頁面中重複出現的,那麼有沒有getElementByClass呢?沒有, 但是可以解決:

//Create an array

var allPageTags = new Array();

function hideDivWithClasses(theClass) {
//Populate the array with all the page tags

var allPageTags=document.getElementsByTagName("div");
//Cycle through the tags using a for loop

for (i=0; i<allPageTags.length;i++)

{

if (allPageTags[i].className==theClass) {
//Manipulate this in whatever way you want
allPageTags[i].style.display='none';
}
}
}

鑒於getElementById()方法使用比較頻繁,我在網上搜集了一下關於此方法的一些用法。

document.getElementById("link").href;
document.getElementById("link").target;
document.getElementById("img").src;
document.getElementById("img").width;
document.getElementById("img").height;
document.getElementById("input").value;
那麼如何取得<div></div>以及<a></a>之間的值呢?如<div id="div">aaa</div>中的aaa,<a href="#" id="link">bbb</a>中的bbb,也很簡單,利用innerHTML就可以了:
document.getElementById("div").innerHTML;
document.getElementById("link").innerHTML;

getElementById 方法
返回具有指定 ID 屬性值的第一個對象的一個引用。
文法
oElement = document.getElementById(sIDValue)
參數
sIDValue 必選項。指明 ID 屬性值的字串
傳回值
返回 ID 屬性值與指定值相同的第一個對象。    //傳回值是一個對象
注釋
如果 ID 屬於一個集合,getElementById 方法返回集合中的第一個對象。
getElementById 方法與使用 all 集合上的 item 方法等同。例如,以下代碼樣本表示如何從 document 對象中取回 ID 為 oDiv 的第一個要素。
使用 DHTML 物件模型:
var oVDiv = document.body.all.item("oDiv");
使用文件物件模型(DOM):
var oVDiv = document.getElementById("oDiv");
樣本

以下例子表示如何使用 getElementById 方法返回 ID 屬性值 oDiv 的第一次出現。
<script>
function fnGetId(){
// Returns the first DIV element in the collection.
var oVDiv=document.getElementById("oDiv1");
}
</script>
<DIV ID="oDiv1">Div #1</DIV>
<DIV ID="oDiv2">Div #2</DIV>
<DIV ID="oDiv3">Div #3</DIV>
<INPUT TYPE="button" VALUE="Get Names" onclick="fnGetId()">
getElementById 方法
返回具有指定 ID 屬性值的第一個比如說有個網頁中有個text框的id叫text1
getElementById(text1)就能得到這個text1框的對象,並使用text框的所有屬性和方法
這個是JS的一個方法,意思是通過控制項ID取得元素的值,如一個form裡包函text、label等,他們都是FORM的元素,有一個分配的ID,getElementById()是取得這些元素的text值的。

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/AC903919/archive/2009/07/31/4396573.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.