getElementByID、createElement、appendChild幾個DHTML元素第1/2頁
更新時間:2008年06月25日 21:17:24 作者:
WEB標(biāo)準(zhǔn)下可以通過getElementById(), getElementsByName(), and getElementsByTagName()訪問
DOCUMENT中的任一個標(biāo)簽:
1、getElementById()
getElementById()可以訪問DOCUMENT中的某一特定元素,顧名思義,就是通過ID來取得元素,所以只能訪問設(shè)置了ID的元素。
比如說有一個DIV的ID為docid:
<div id="docid"></div>
那么就可以用getElementById("docid")來獲得這個元素。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>ById</title>
<style type="text/css">
<!--
#docid{
height:400px;
width:400px;
background-color:#999;}
-->
</style>
</head>
<body><div id="docid" name="docname" onClick="bgcolor()"></div>
</body>
</html>
<script language="JavaScript" type="text/JavaScript">
<!--
function bgcolor(){
document.getElementById("docid").style.backgroundColor="#000"
}
-->
</script>
、getElementsByName()
這個是通過NAME來獲得元素,但不知大家注意沒有,這個是GET ELEMENTS,復(fù)數(shù)ELEMENTS代表獲得的不是一個元素,為什么呢?
因為DOCUMENT中每一個元素的ID是唯一的,但NAME卻可以重復(fù)。打個比喻就像人的身份證號是唯一的(理論上,雖然現(xiàn)實中有重復(fù)),但名字
重復(fù)的卻很多。如果一個文檔中有兩個以上的標(biāo)簽NAME相同,那么getElementsByName()就可以取得這些元素組成一個數(shù)組。
比如有兩個DIV:
<div name="docname" id="docid1"></div>
<div name="docname" id="docid2"></div>
那么可以用getElementsByName("docname")獲得這兩個DIV,用getElementsByName("docname")[0]訪問第一個DIV,用getElementsByName
3、getElementsByTagName()
這個呢就是通過TAGNAME(標(biāo)簽名稱)來獲得元素,一個DOCUMENT中當(dāng)然會有相同的標(biāo)簽,所以這個方法也是取得一個數(shù)組。
下面這個例子有兩個DIV,可以用getElementsByTagName("div")來訪問它們,用getElementsByTagName("div")[0]訪問第一個DIV,用
getElementsByTagName("div")[1]訪問第二個DIV。
<!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>
總結(jié)一下標(biāo)準(zhǔn)DOM,訪問某一特定元素盡量用標(biāo)準(zhǔn)的getElementById(),訪問標(biāo)簽用標(biāo)準(zhǔn)的getElementByTagName(),但I(xiàn)E不支持
getElementsByName(),所以就要避免使用getElementsByName(),但getElementsByName()和不符合標(biāo)準(zhǔn)的document.all[]也不是全無是處,它
們有自己的方便之處,用不用那就看網(wǎng)站的用戶使用什么瀏覽器,由你自己決定了。
Javascript中的getElementById十分常用,但在標(biāo)準(zhǔn)的頁面中,一個id只能出現(xiàn)一次,如果我想同時控制多個元素,例如點(diǎn)一個鏈接, 讓多個層隱藏,該怎么做?用class,當(dāng)然,同一個class是可以允許在頁面中重復(fù)出現(xiàn)的,那么有沒有g(shù)etElementByClass呢?沒有, 但是可以解決:
//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//Pick out the tags with our class name
if (allPageTags[i].className==theClass) {
//Manipulate this in whatever way you want
allPageTags[i].style.display='none';
}
}
}
=============================
1、getElementById()
getElementById()可以訪問DOCUMENT中的某一特定元素,顧名思義,就是通過ID來取得元素,所以只能訪問設(shè)置了ID的元素。
比如說有一個DIV的ID為docid:
<div id="docid"></div>
那么就可以用getElementById("docid")來獲得這個元素。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>ById</title>
<style type="text/css">
<!--
#docid{
height:400px;
width:400px;
background-color:#999;}
-->
</style>
</head>
<body><div id="docid" name="docname" onClick="bgcolor()"></div>
</body>
</html>
<script language="JavaScript" type="text/JavaScript">
<!--
function bgcolor(){
document.getElementById("docid").style.backgroundColor="#000"
}
-->
</script>
、getElementsByName()
這個是通過NAME來獲得元素,但不知大家注意沒有,這個是GET ELEMENTS,復(fù)數(shù)ELEMENTS代表獲得的不是一個元素,為什么呢?
因為DOCUMENT中每一個元素的ID是唯一的,但NAME卻可以重復(fù)。打個比喻就像人的身份證號是唯一的(理論上,雖然現(xiàn)實中有重復(fù)),但名字
重復(fù)的卻很多。如果一個文檔中有兩個以上的標(biāo)簽NAME相同,那么getElementsByName()就可以取得這些元素組成一個數(shù)組。
比如有兩個DIV:
<div name="docname" id="docid1"></div>
<div name="docname" id="docid2"></div>
那么可以用getElementsByName("docname")獲得這兩個DIV,用getElementsByName("docname")[0]訪問第一個DIV,用getElementsByName
3、getElementsByTagName()
這個呢就是通過TAGNAME(標(biāo)簽名稱)來獲得元素,一個DOCUMENT中當(dāng)然會有相同的標(biāo)簽,所以這個方法也是取得一個數(shù)組。
下面這個例子有兩個DIV,可以用getElementsByTagName("div")來訪問它們,用getElementsByTagName("div")[0]訪問第一個DIV,用
getElementsByTagName("div")[1]訪問第二個DIV。
<!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>
總結(jié)一下標(biāo)準(zhǔn)DOM,訪問某一特定元素盡量用標(biāo)準(zhǔn)的getElementById(),訪問標(biāo)簽用標(biāo)準(zhǔn)的getElementByTagName(),但I(xiàn)E不支持
getElementsByName(),所以就要避免使用getElementsByName(),但getElementsByName()和不符合標(biāo)準(zhǔn)的document.all[]也不是全無是處,它
們有自己的方便之處,用不用那就看網(wǎng)站的用戶使用什么瀏覽器,由你自己決定了。
Javascript中的getElementById十分常用,但在標(biāo)準(zhǔn)的頁面中,一個id只能出現(xiàn)一次,如果我想同時控制多個元素,例如點(diǎn)一個鏈接, 讓多個層隱藏,該怎么做?用class,當(dāng)然,同一個class是可以允許在頁面中重復(fù)出現(xiàn)的,那么有沒有g(shù)etElementByClass呢?沒有, 但是可以解決:
//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//Pick out the tags with our class name
if (allPageTags[i].className==theClass) {
//Manipulate this in whatever way you want
allPageTags[i].style.display='none';
}
}
}
=============================
您可能感興趣的文章:
- 原生js操作checkbox用document.getElementById實現(xiàn)
- 一行代碼告別document.getElementById
- js querySelector和getElementById通過id獲取元素的區(qū)別
- getElementByIdx_x js自定義getElementById函數(shù)
- document.getElementById介紹
- document.getElementById方法在Firefox與IE中的區(qū)別
- document.getElementById為空或不是對象的解決方法
- document.getElementById獲取控件對象為空的解決方法
相關(guān)文章
js 禁止選擇功能實現(xiàn)代碼(兼容IE/Firefox)
有時候出于某種需要,不希望用戶可以選擇某個區(qū)域,進(jìn)行下面的操作,這里給出簡單的代碼。2010-04-04JavaScript 小型打飛機(jī)游戲?qū)崿F(xiàn)原理說明
這次為大家?guī)淼男∮螒蚴牵捍蝻w機(jī)。呃。。。我本人就寫不出什么驚天大作的游戲的了,只能寫寫小游戲,代碼量小,又可以學(xué)習(xí),主要是想法思路,代碼量大,估計也沒啥人會去研究學(xué)習(xí)。。。2010-10-10關(guān)于JS判斷圖片是否加載完成且獲取圖片寬度的方法
本篇文章小編為大家介紹,關(guān)于JS判斷圖片是否加載完成且獲取圖片寬度的方法,有需要的朋友可以參考一下2013-04-04javascript內(nèi)嵌式與外鏈?zhǔn)降幕緫?yīng)用方式
這篇文章主要介紹了javascript內(nèi)嵌式與外鏈?zhǔn)降幕緫?yīng)用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12JavaScript的內(nèi)置對象Math和字符串詳解
這篇文章主要為大家介紹了JavaScript的內(nèi)置對象Math和字符串,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-11-11