1. 程式人生 > >DOM模型入門-getElementById()案例教程

DOM模型入門-getElementById()案例教程

<!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>getElementsByTagName</title>
<script language="javascript">
function searchDOM(){
//文件的DOM結構必須是在整個文件載入完畢後才能正確分析出來,因此放在函式內,頁面載入完成後才用<body>的onload載入

 var oLi=document.getElementById("cssLi");

 alert(oLi.tagName+","+oLi.childNodes[0].nodeValue);
 //輸出標籤名稱,以及文字節點的值
}
</script>
</head>

<body onload="searchDOM()">
<ul>客戶端語言
  <li>HTML</li>
  <li>JavaScript</li>
  <li id="cssLi">CSS</li>  //id在這裡
</ul>
<ul>伺服器端語言
  <li id="cssLi">ASP.NET</li>
  <li>JSP</li>
  <li>PHP</li>
</ul>

</body>
</html>

執行結果: