|
藍森林 http://www.lslnet.com 2006年8月25日 8:28
細說document.all[]與WEB標準
來BLUEIDEA已經一年了,不過第一次發教程啊,好不好大家給個意見。不知寫的有沒有價值,如果不錯的話請求版主置頂。閒話少說,進入正題:
[color=#FF0000]1、DOM[/color]
WEB標準現在可真是熱門中熱門,不過下面討論的是一個不符合標準的document.all[]。
DOM--DOCUMENT OBJECT MODEL文檔對像模型,提供了訪問文檔對象的方法.
例如文檔中有一個table,你要改變它的背景顏色,那就可以在JAVASCRIPT中用document.all[]訪問這個TABLE。但DOM也有所不同,因
為瀏覽器廠商之間的競爭,各瀏覽器廠商都開發了自己的私有DOM,只能在自己的瀏覽器上正確運行,document.all[]就是只能運行在 IE是
的微軟的私有DOM。為了正確理解DOM,給出IE4的DOM
[img]http://202.113.126.156/freeweb/dom.jpg[/img]
[color=#FF0000]2、理解document.all[][/color]
從IE4開始IE的object model才增加了document.all[],來看看document.all[]的Description:
Array of all HTML tags in the document.Collection of all elements contained by the object.
也就是說document.all[]是文檔中所有標籤組成的一個數組變量,包括了文檔對像中所有元素(見例1)。
IE』s document.all collection exposes all document elements.This array provides access to every element in the
document.
document.all[]這個數組可以訪問文檔中所有元素。
例1(這個可以讓你理解文檔中哪些是對像)
[html]
<!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>
[/html]
例2(訪問一個特定元素)
[html]
<!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>單擊DIV變色</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.all[7].style.backgroundColor="#000"
}
-->
</script>
[/html]
上面的這個例子讓你瞭解怎麼訪問文檔中的一個特定元素,比如文檔中有一個DIV
<div id="docid" name="docname"></div>
你可以通過這個DIV的ID,NAME或INDEX屬性訪問這個DIV:
document.all["docid"]
document.all["docname"]
document.all.item("docid")
document.all.item("docname")
document.all[7]
document.all.tags("div")則返回文檔中所有DIV數組,本例中只有一個DIV,所以用document.all.tags("div")[0]就可以訪問了。
[color=#FF0000]3、使用document.all[][/color]
例3
[html]
<!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 #2</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
<!-- Works in Internet Explorer and compatible -->
<h1 id="heading1" align="center" style="font-size: larger;">DHTML Fun!!!</h1>
<form name="testform" id="testform" action="#" method="get">
<br /><br />
<input type="button" value="Align Left"
onclick="document.all['heading1'].align='left';" />
<input type="button" value="Align Center"
onclick="document.all['heading1'].align='center';" />
<input type="button" value="Align Right"
onclick="document.all['heading1'].align='right';" />
<br /><br />
<input type="button" value="Bigger"
onclick="document.all['heading1'].style.fontSize='xx-large';" />
<input type="button" value="Smaller"
onclick="document.all['heading1'].style.fontSize='xx-small';" />
<br /><br />
<input type="button" value="Red"
onclick="document.all['heading1'].style.color='red';" />
<input type="button" value="Blue"
onclick="document.all['heading1'].style.color='blue';" />
<input type="button" value="Black"
onclick="document.all['heading1'].style.color='black';" />
<br /><br />
<input type="text" name="userText" id="userText" size="30" />
<input type="button" value="Change Text"
onclick="document.all['heading1'].innerText=document.testform.userText.value;" />
</form>
</body>
</html>
[/html]
[color=#FF0000]4、標準DOM中的訪問方法[/color]
開頭就說過document.all[]不符合WEB標準,那用什麼來替代它呢?document.getElementById
Most third-party browsers are 「strict standards」 implementations, meaning that they implement W3C and ECMA standards and
ignore most of the proprietary object models of Internet Explorer and Netscape.
If the demographic for your Web site includes users likely to use less common browsers, such as Linux aficionados, it might
be a good idea to avoid IE-specific features and use the W3C DOM instead.
by Internet Explorer 6, we see that IE implements significant portions of the W3C DOM.
這段話的意思是大多數第三方瀏覽器只支持W3C的DOM,如果你的網站用戶使用其他的瀏覽器,那麼你最好避免使用IE的私有屬性。而且IE6也開
始支持W3C DOM。
畢竟大多數人還不瞭解標準,在使用標準前,你還可以在你的網頁中用document.all[]訪問文檔對象。(待續)
|
細說document.all[]
好啊,原來document.all[]是不標準的,用getElementById是標準的。
正好想多瞭解這方面的東西,今天真是碰上了。
樓主繼續寫啊,支持一下:D |
細說document.all[]
好文章 |
細說document.all[]
前面寫到WEB標準,今天繼續
WEB標準下可以通過getElementById(), getElementsByName(), and getElementsByTagName()訪問DOCUMENT中的任一個標籤:
[color="#FF0000"]1、getElementById()[/color]
getElementById()可以訪問DOCUMENT中的某一特定元素,顧名思義,就是通過ID來取得元素,所以只能訪問設置了ID的元素。
比如說有一個DIV的ID為docid:
<div id="docid"></div>
那麼就可以用getElementById("docid")來獲得這個元素。
[html]
<!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>
[/html]
[color="#FF0000"]2、getElementsByName()[/color]
這個是通過NAME來獲得元素,但不知大家注意沒有,這個是GET ELEMENTS,複數ELEMENTS代表獲得的不是一個元素,為什麼呢?
因為DOCUMENT中每一個元素的ID是唯一的,但NAME卻可以重複。打個比喻就像人的身份證號是唯一的(理論上,雖然現實中有重複),但名字
重複的卻很多。如果一個文檔中有兩個以上的標籤NAME相同,那麼getElementsByName()就可以取得這些元素組成一個數組。
比如有兩個DIV:
<div name="docname" id="docid1"></div>
<div name="docname" id="docid2"></div>
那麼可以用getElementsByName("docname")獲得這兩個DIV,用getElementsByName("docname")[0]訪問第一個DIV,用getElementsByName
("docname")[1]訪問第二個DIV。
[color=#ff0000][b]下面這段話有錯,請看forfor的回復[/b]
但是很可惜,IE沒有支持這個方法,大家有興趣可以在FIREFOX或NETSCAPE中調試下面這個例子。(我在NETSCAPE7.2英文版和FIREFOX1.0中調
試成功。)
<!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>
<div name="docname" id="docid1" onClick="bgcolor()"></div>
<div name="docname" id="docid2" onClick="bgcolor()"></div>
</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";
}
-->
</script>
[/color]
[color=#0000ff]看來最新版瀏覽器理解WEB標準還是有問題,我知道的只有盒模型、空格BUG、漂浮BUG、FLASH插入BUG,從document.getElementsByName可以看出FIREFOX,NETSCAPE理解標準有偏差,但forfor說的對:要靈活應用標準。[/color]
[color="#FF0000"]3、getElementsByTagName()[/color]
這個呢就是通過TAGNAME(標籤名稱)來獲得元素,一個DOCUMENT中當然會有相同的標籤,所以這個方法也是取得一個數組。
下面這個例子有兩個DIV,可以用getElementsByTagName("div")來訪問它們,用getElementsByTagName("div")[0]訪問第一個DIV,用
getElementsByTagName("div")[1]訪問第二個DIV。
[html]
<!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>
<div name="docname" id="docid1" onClick="bgcolor()"></div>
<div name="docname" id="docid2" onClick="bgcolor()"></div>
</body>
</html>
<script language="JavaScript" type="text/JavaScript">
<!--
function bgcolor(){
var docnObj=document.getElementsByTagName("div");
docnObj[0].style.backgroundColor = "black";
docnObj[1].style.backgroundColor = "black";
}
-->
</script>
[/html]
總結一下標準DOM,訪問某一特定元素盡量用標準的getElementById(),訪問標籤用標準的getElementByTagName(),但IE不支持
getElementsByName(),所以就要避免使用getElementsByName(),但getElementsByName()和不符合標準的document.all[]也不是全無是處,它
們有自己的方便之處,用不用那就看網站的用戶使用什麼瀏覽器,由你自己決定了。 |
細說document.all[]與WEB標準[請求置頂]
樓主提到:
[html]
<!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>Test</title>
<style type="text/css">
<!--
#docname{
margin:10px;
height:400px;
width:400px;
background-color:#999;}
-->
</style>
</head>
<body>
<div id="docname" onClick="bgcolor()"></div>
<div id="docname" onClick="bgcolor()"></div>
</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";
}
-->
</script>
</html>
[/html]
印象中以前也遇到這個問題,id屬性可以通過。
[size=3][b]getElementsByName Method [/b][/size]
...
[b]remarks[/b]
When you use the getElementsByName method, all elements in the document that have the specified NAME or ID attribute value are returned.
Elements that support both the NAME and the ID attribute are included in the collection returned by the getElementsByName method, but not elements with a NAME expando.
|
細說document.all[]與WEB標準[請求置頂]
關於document.getElementsByName
IE當然支持 可以說IE更忠於html/xhtml標準(嘿嘿 原來firefox也不咋地 幸災樂禍一下^_^)
按照O'REILLY的<<HTML與XHTML權威指南>>中的說法 name並不是核心屬性 並非所有標籤都可以加name屬性(大家可以拿我下面的例子去 http://validator.w3.org 做驗證)
所以你給div加name屬性理論上是不會出結果的.這一點IE很好的符合了標準~!!
(同時也看出了符合標準也有煩人的地方~_~ 所以大家不用太把標準當回事兒 過兩年都用xml了 這個也過時了!倡導靈活的webstandard應用思想 除了符合xml思想的東西 其他的按瀏覽器的理解去做就行)
附:
[html]
<!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>
[/html] |
細說document.all[]與WEB標準[請求置頂]
並非所有標籤都可以加name屬性,沒有注意這一點,沒有好好測試,對不起黨和人民,我有罪。
差點誤導別人,呵呵
forfor說的有道理
|
細說document.all[]與WEB標準[請求置頂]
有簡單點的講解嗎? 這個 講解的我暈頭轉向的
看不太懂 |
細說document.all[]與WEB標準[請求置頂]
簡單來說就是DIV不支持NAME屬性,所以那個document.getElementsByName的例子調試不能通過.
下面用INPUT做個例子
[html]
<!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>
[/html] |
細說document.all[]與WEB標準[請求置頂]
我的意思是 你開始的第一個教程我就沒看懂
能不能像這樣的http://dev.csdn.net/article/61/61507.shtm
Window.open的詳解
這樣的教程 好看一些 |
細說document.all[]與WEB標準[請求置頂]
有空看看,不過今天坐車回學校20個小時啊,累
沒空改了 |
頂上去,難得看到這麼好的關於JS的討論 |
期待更多這樣的好文章 |
嚴格來說IE的getElementsByName是不符合標準的,這個問題我在Firefox論壇討論過:
[url=http://www.firefox.net.cn/newforum/viewtopic.php?t=9694]ie和firefox中的getElementsByName的不同[轉載,遇到同樣的問題] |
BolerGuo在上個帖子中說
|
第一個寫錯了,下面的鏈接是對的,怎麼不能刪除?
http://www.firefox.net.cn/newforum/viewtopic.php?t=7441 |
看不懂啊!! |
感覺不錯,還沒仔細看呢 |
|