引用文档中的HTML元素的3种方法
document对象的all属性返回值包含文档中所有HTML标签的数组,对all[]数组中元素的引用方法有三种,分别为document.all[i]、document.all[name]、以及document.all.tags[tagName]
代码实例:
<body>
<!--document.all[]返回了一个数组,数组中的元素为HTML文档中的所有HTML元素 -->
<script language="javascript">
function Value(){
var str=new Array(3);
str[0]=document.all.cnlText1.value;//第一种方法
str[1]=document.all["cnlText2"].value;//第二种方法
str[2]=document.all.tags('form')[0].cnlText3.value;//第三种方法(不明白)
with(document){//访问document对象的属性值
write("姓名:"+str[0]+"<br>");
write("住址:"+str[1]+"<br>");
write("工号:"+str[2]+"<br>");
}
}
</script>
<form action="" method="get" name="form1">
<p> </P>
<p>姓名:
<input id="cnlText1" name="cnlText1" value="">
</p>
<p>住址:
<input id="cnlText2" name="cnlText2" value="">
</p>
<p>工号:
<input id="cnlText3" name="cnlText3" value="">
</p>
<p>
<input name="" type="Submit" onClick="Value()" value="显示信息">
</p>
</body>