现在的位置: 首页 > 综合 > 正文

ie中用document.all而firefox中不识,则用document.getElementByTagName(“*”)

2019年11月16日 ⁄ 综合 ⁄ 共 2452字 ⁄ 字号 评论关闭

document.all
[
]是文档中所有标签组成的一个数组变量,包括了文档对象中所有元素

document.all
[
]这个数组可以访问文档中所有元素。

例1(这个可以让你理解文档中哪些是对象)

<!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>
<title>Document.All
Example</title>
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1" />
</head>
<body>
<h1>Example Heading</h1>
<hr />
<p>This is a <em>paragraph</em>. It is only a
<em>paragraph.</em></p>
<p>Yet another <em>paragraph.</em></p>
<p>This final <em>paragraph</em> has <em
id="special">special emphasis.</em></p>
<hr />
<script type="text/JavaScript"> .

<!--
var i,origLength;
origLength = document.all
.length;
document.write('document.all
.length='
origLength "<br />");
for (i = 0; i < origLength; i )
{
document.write("document.all
[" i "]=" document.all
[i].tagName "<br />");
}
//-->
</script>
</body>
</html>

输出结果:

document.all.length=18
document.all[0]=!
document.all[1]=HTML
document.all[2]=HEAD
document.all[3]=TITLE
document.all[4]=META
document.all[5]=BODY
document.all[6]=H1
document.all[7]=HR
document.all[8]=P
document.all[9]=EM
document.all[10]=EM
document.all[11]=P
document.all[12]=EM
document.all[13]=P
document.all[14]=EM
document.all[15]=EM
document.all[16]=HR
document.all[17]=SCRIPT

 

 

<body>
            <div id="aa">123456</div>
            <input type="button" value="这里用 document 就出错"
            onclick="alert(document.aa.innerText);" />
            <br />
            <input type="button" value="这里用 document.all 就不出错"
            onclick="alert(document.all.aa.innerText);" />
            </body> 运行以上代码.

           
如果与a,form对象,image对象,applet对象相对应的html标记中设定了name性质,它的值将被用作document对象的属性名,用
来引用相应的对象,其他的对象则不可以。

           
另外,input等如果作为form的子元素,则直接用inputName或者document.inputName来引用此对象就是错误的,必须使用
formName.inputName引用,否则就可以使用inputName来引用.

            另外应该注意到有很多平时用的元素都没有name.
           
如果想引用一个有id的元素,只能用Id或者document.getElementById,document.all.id来引用
            但是象这样的元素,所以象<a href="......" name="linkname"
            id="linkid">......</a>这样的
            可以用
            linkid.href;
            linkname.href;
            document.all.linkid.href;
            document.all.linkname.href;
            document.getElementById("linkid").href;
            document.getElementsByName("linkname")[0].href来引用
            all是一个集合,包含所有html对像的集合,写一个程式,可以存取到所有的对像。像这样:
            <script language="javascript">
            var obj="";
            for(i=0;i<document.all.length;i++)
            obj+=document.all[i].tagName+";";
            alert(obj);
            </script>
            注意要把程式放到</html>之后哦。

抱歉!评论已关闭.