网管

返回首页
当前位置: 网管>网站建设>php>

getElementById,getElementsByName,getElementsByTagName的用

时间:2009-09-15 23:44来源:未知 作者:admin 点击:
getElementById: 语法: document.getElementById(id) 参数:id :必选项为字符串(String) 返回值:对象; 返回相同id对象中的第一个,如果无符合条件的对象,则返回 null example:document.getElementById(id1).value; getEleme
  

getElementById:
语法: document.getElementById(id)
参数:id :必选项为字符串(String)
返回值:对象; 返回相同id对象中的第一个,如果无符合条件的对象,则返回 null
example:document.getElementById("id1").value;


getElementsByName:
语法: document.getElementsByName(name)
参数:name :必选项为字符串(String)
返回值:数组对象; 如果无符合条件的对象,则返回空数组
example:document.getElementsByName("name1")[0].value;
document.getElementsByName("name1")[1].value;


getElementsByTagName:
语法: object.getElementsByTagName(tagname) object可以是document或

event.srcElement.parentElement等
参数:tagname:必选项为字符串(String)
返回值:数组对象; 如果无符合条件的对象,则返回空数组
example:document.getElementsByTagName("p")[0].childNodes[0].nodeValue;
document.getElementsByTagName("p")[1].childNodes[0].nodeValue;

--------------------------------------------------------------------------------------------

-----------------------------------

WEB标准下可以通过getElementById(), getElementsByName(), and getElementsByTagName()访问

DOCUMENT中的任一个标签:

1、getElementById()
getElementById()可以访问DOCUMENT中的某一特定元素,顾名思义,就是通过ID来取得元素,所以只能访

问设置了ID的元素。
比如说有一个DIV的ID为docid:
<div id="docid"></div>
那么就可以用getElementById("docid")来获得这个元素。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<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>
2、getElementsByName()
这个是通过NAME来获得元素,但不知大家注意没有,这个是GET ELEMENTS,复数ELEMENTS代表获得的不是

一个元素,为什么呢?
因为DOCUMENT中每一个元素的ID是唯一的,但NAME却可以重复。打个比喻就像人的身份证号是唯一的(理

论上,虽然现实中有重复),但名字重复的却很多。如果一个文档中有两个以上的标签NAME相同,那么

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
最新评论 查看所有评论
发表评论 查看所有评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
推荐内容