网络编程
位置:首页>> 网络编程>> JavaScript>> document.all与WEB标准(3)

document.all与WEB标准(3)

  发布时间:2008-03-16 14:28:00 

标签:dom,document,标准,web,javascript


  总结一下标准DOM,访问某一特定元素尽量用标准的getElementById(),访问标签用标准的getElementByTagName(),但IE不支持getElementsByName(),所以就要避免使用getElementsByName(),但getElementsByName()和不符合标准的document.all[]也不是全无是处,它们有自己的方便之处,用不用那就看网站的用户使用什么浏览器,由你自己决定了。

  关于document.getElementsByName

  IE当然支持 可以说IE更忠于html/xhtml标准(嘿嘿 原来firefox也不咋地 幸灾乐祸一下^_^)

  按照O'REILLY的<<HTML与XHTML权威指南>>中的说法 name并不是核心属性 并非所有标签都可以加name属性(大家可以拿我下面的例子去 validator.w3.org 做验证)

  所以你给div加name属性理论上是不会出结果的.这一点IE很好的符合了标准~!!

  (同时也看出了符合标准也有烦人的地方~_~ 所以大家不用太把标准当回事儿 过两年都用xml了 这个也过时了!倡导灵活的webstandard应用思想 除了符合xml思想的东西 其他的按浏览器的理解去做就行)

附:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<script type="text/javascript">
<!--
function aa(){
var s="Elements with attribute 'name':\n";
var aaa=document.getElementsByName("a");
for(var i=0;i<aaa.length;i++)s+="\n"+aaa[i].tagName;
alert(s);
}
-->
</script>
<title> test </title>
</head>
<body>
<div name="a"><span name="a">1</span>2<input name="a" value="3"/><textarea name="a" rows="2" cols="8">4</textarea><button onclick="aa()">alert</button></div>
</body>
</html>



  简单来说就是DIV不支持NAME属性,所以那个document.getElementsByName的例子调试不能通过.下面用INPUT做个例子

<!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>
<input name="docname" onmouseover="bgcolor()" onmouseout="bgcolor2()" />
<input name="docname" onmouseover="bgcolor()" onmouseout="bgcolor2()" />
</body>
</html>
<script language="javascript" type="text/javascript">
<!--
function bgcolor(){
var docnObj=document.getElementsByName("docname");
docnObj[0].style.backgroundColor = "black";
docnObj[1].style.backgroundColor = "black";
}
function bgcolor2(){
var docnObj=document.getElementsByName("docname");
docnObj[0].style.backgroundColor = "#fff";
docnObj[1].style.backgroundColor = "#fff";
}
-->
</script>
0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com