DOCUMENT中的任一個標簽:
1、getElementById()
getElementById()可以訪問DOCUMENT中的某一特定元素,顧名思義,就是通過ID來取得元素,所以只能訪問設置了ID的元素。
比如說有一個p的ID為docid:
<p id="docid"></p>
那么就可以用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><p id="docid" name="docname" onClick="bgcolor()"></p> </body> </html> <script language="JavaScript" type="text/JavaScript"> <!-- function bgcolor(){ document.getElementById("docid").style.backgroundColor="#000" } --> </script>
、getElementsByName()
這個是通過NAME來獲得元素,但不知大家注意沒有,這個是GET ELEMENTS,復數ELEMENTS代表獲得的不是一個元素,為什么呢?
因為DOCUMENT中每一個元素的ID是唯一的,但NAME卻可以重復。打個比喻就像人的身份證號是唯一的(理論上,雖然現實中有重復),但名字
重復的卻很多。如果一個文檔中有兩個以上的標簽NAME相同,那么getElementsByName()就可以取得這些元素組成一個數組。
比如有兩個p:
<p name="docname" id="docid1"></p>
<p name="docname" id="docid2"></p>
那么可以用getElementsByName("docname")獲得這兩個p,用getElementsByName("docname")[0]訪問第一個p,用getElementsByName
3、getElementsByTagName()
這個呢就是通過TAGNAME(標簽名稱)來獲得元素,一個DOCUMENT中當然會有相同的標簽,所以這個方法也是取得一個數組。
下面這個例子有兩個p,可以用getElementsByTagName("p")來訪問它們,用getElementsByTagName("p")[0]訪問第一個p,用
getElementsByTagName("p")[1]訪問第二個p。
<!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> <p name="docname" id="docid1" onClick="bgcolor()"></p> <p name="docname" id="docid2" onClick="bgcolor()"></p> </body> </html> <script language="JavaScript" type="text/JavaScript"> <!-- function bgcolor(){ var docnObj=document.getElementsByTagName("p"); 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 hidepWithClasses(theClass) { //Populate the array with all the page tags var allPageTags=document.getElementsByTagName("p"); //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'; } } }
=============================
appendChild方法的使用
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>無標題文檔</title> </head> <script language="javascript"> //生成與輸入內容匹配行 function setNames() { completeBody = document.getElementById("complete_body"); var row, cell, txtNode; //var nextNode = names[i].firstChild.data; row = document.createElement("tr"); cell = document.createElement("td"); cell.setAttribute("bgcolor", "#FFFAFA"); cell.setAttribute("border", "0"); //txtNode = document.createTextNode(nextNode); alert("sdf"); var newText = document.createTextNode("This is the second paragraph."); //txtNode=document.createElement("p"); alert("sdf1"); cell.appendChild(newText); alert("sdf2"); row.appendChild(cell); completeBody.appendChild(row); } </script> <body> <input type="submit" name="sdf" onclick="setNames()"> <table id="complete_table" bgcolor="#FFFAFA" border="0" cellspacing="0" cellpadding="0" /> <tbody id="complete_body"></tbody> </table> </body> </html>
=================================
createElement
<html> <head> <title>createElement</title> <script language="javascript"> <!-- var i=0 ; function addInput() { var o = document.createElement("input"); o.type = "button" ; o.value = "按鈕" + i++ ; o.attachEvent("onclick",addInput); document.body.appendChild(o); o = null; } //--> </script> </head> <body onload="addInput();"> </body> </html>
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com