淺談html中id和name的區(qū)別實例代碼
更新時間:2008年07月28日 23:00:55 作者:
這個是form里面的name與id的區(qū)別
我們可以通過一段代碼來分析一下其中的微妙差別:
<form method="post" action="" name="demoform">
<input type="text" name="oDemo" id=”oDemo2” value="DEMO" />
</form>
在IE瀏覽器里,我們可以通過多少方法來索引到這個文本框?qū)ο螅?為區(qū)別起見,我們把NAME和ID設(shè)成了不同的值)
1. oDemo
2. demoform.oDemo
3. document.all.oDemo
4. document.all.demoform.oDemo
5. document.forms[0].oDemo
6. document.forms['demoform'].oDemo
7. document.forms['demoform'].childNodes[0]
8. document.forms['demoform'].elements[0]
9. document.getElementById('oDemo2')
以上9種索引方法在IE6里面全部通過返回值測試,不過值得注意的是最后一種:在IE6里,我把索引對象寫成
document.getElementById('oDemo'),瀏覽器也能正確索引到對象,真是可怕的容錯性?。。?
接著問題來了,我們把這段代碼放在Mozilla Firefox 1.0里再執(zhí)行一次,只有第7種方法返回“undefined”,其他的方法可以正確
索引到對象,不過由于第3、4種方法用到了document.all這個IE專有對象,F(xiàn)F1.0雖然返回了正確的值,不過卻在控制臺里發(fā)出了警
告:警告:非標準的屬性 document.all。請使用 W3C 的標準形式 document.getElementById() 。
接下來我們把HTML文本類型定義得嚴格一點,在源代碼開頭加上:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
使HTML文本按照HTML4.01標準去解析,在IE6里照樣全部通過返回值測試,不過在Mozilla Firefox 1.0里麻煩就大了,第3、4種方法
沒有任何的返回值,而在控制臺里發(fā)出了報錯信息:錯誤: document.all has no properties ,而第7種方法依舊返回“undefined
”。
小 結(jié)
name原來是為了標識之用,但是現(xiàn)在根據(jù)規(guī)范,都建議用id來標識元素。
以下只能用name:
1. 表單(form)的控件名,提交的數(shù)據(jù)都用控件的name而不是id來控制。因為有許多name會同時對應(yīng)多個控件,比如
checkbox和radio,而id必須是全文檔中唯一的。此外瀏覽器會根據(jù)name來設(shè)定發(fā)送到服務(wù)器的request。因此如果用id,服務(wù)器是無
法得到數(shù)據(jù)的。
2. frame和window的名字,用于在其他frame或window指定target。
以下只能用id:
1. label與form控件的關(guān)聯(lián),
<label for="MyInput">My Input</label>
<input id="MyInput" type="text">
for屬性指定與label關(guān)聯(lián)的元素的id,不可用name替代。
2. CSS的元素選擇機制,以#MyId的方式指定應(yīng)用樣式的元素,不能用name替代。
3. 腳本中獲得對象:
IE支持在腳本中直接以id(而不是name)引用該id標識的對象。例如上面的input,要在腳本中獲得輸入的內(nèi)容,可以直接以
MyInput.value來獲得。
如果用DOM的話,則用document.getElementById("MyInput").value,如果要用name的話,通常先得到包含控件的form,例如
document.forms[0],然后從form再引用name,注意這樣得到的是經(jīng)過計算后將發(fā)送給服務(wù)器的值。
name與id的其他區(qū)別是:
id要符合標識的要求,比如大小寫敏感,最好不要包含下劃線(因為不兼容CSS)。而name基本上沒有什么要求,甚至可以用數(shù)字
。
用CSS控制這個鏈接的停留樣式,
可以這樣寫 #m_blog div.opt a:hover{color:#D57813} 或 #myLink:hover{color:#D57813}
NAME主要應(yīng)用在交互式網(wǎng)頁,表單提交給某個服務(wù)器端腳本后接收變處理量使用。從源代碼的規(guī)范性和兼容性角度出發(fā),如在客戶端
腳本里要索引某個對象,建議用document.getElementById()方法,盡量不要直接使用NAME的值,當(dāng)然如果不考慮兼容性,以上9種方
法都可以在IE里運行通過(IE5.0沒測試過)。
附:測試源代碼
<!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></title>
</head>
<body>
<form method="post" action="" name="demoform">
<input type="text" name="oDemo" value="DEMO" id="oDemo2" /><br />
<input type="button" value="oDemo" onclick="alert(oDemo.value)" /><br />
<input type="button" value="demoform.oDemo" onclick="alert(demoform.oDemo.value)" /><br />
<input type="button" value="document.all.oDemo" onclick="alert(document.all.oDemo.value)" /><br />
<input type="button" value="document.all.demoform.oDemo" onclick="alert(document.all.demoform.oDemo.value)" /><br />
<input type="button" value="document.forms[0].oDemo" onclick="alert(document.forms[0].oDemo.value)" /><br />
<input type="button" value="document.forms['demoform'].oDemo" onclick="alert(document.forms['demoform'].oDemo.value)" /><br />
<input type="button" value="document.forms['demoform'].childNodes[0]" onclick="alert(document.forms
['demoform'].childNodes[0].value)" /><br />
<input type="button" value="document.forms['demoform'].elements[0]" onclick="alert(document.forms
['demoform'].elements[0].value)" /><br />
<input type="button" value="document.getElementById('oDemo2')" onclick="alert(document.getElementById('oDemo2').value)" /><br />
</form>
</body>
</html>
<form method="post" action="" name="demoform">
<input type="text" name="oDemo" id=”oDemo2” value="DEMO" />
</form>
在IE瀏覽器里,我們可以通過多少方法來索引到這個文本框?qū)ο螅?為區(qū)別起見,我們把NAME和ID設(shè)成了不同的值)
1. oDemo
2. demoform.oDemo
3. document.all.oDemo
4. document.all.demoform.oDemo
5. document.forms[0].oDemo
6. document.forms['demoform'].oDemo
7. document.forms['demoform'].childNodes[0]
8. document.forms['demoform'].elements[0]
9. document.getElementById('oDemo2')
以上9種索引方法在IE6里面全部通過返回值測試,不過值得注意的是最后一種:在IE6里,我把索引對象寫成
document.getElementById('oDemo'),瀏覽器也能正確索引到對象,真是可怕的容錯性?。。?
接著問題來了,我們把這段代碼放在Mozilla Firefox 1.0里再執(zhí)行一次,只有第7種方法返回“undefined”,其他的方法可以正確
索引到對象,不過由于第3、4種方法用到了document.all這個IE專有對象,F(xiàn)F1.0雖然返回了正確的值,不過卻在控制臺里發(fā)出了警
告:警告:非標準的屬性 document.all。請使用 W3C 的標準形式 document.getElementById() 。
接下來我們把HTML文本類型定義得嚴格一點,在源代碼開頭加上:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
使HTML文本按照HTML4.01標準去解析,在IE6里照樣全部通過返回值測試,不過在Mozilla Firefox 1.0里麻煩就大了,第3、4種方法
沒有任何的返回值,而在控制臺里發(fā)出了報錯信息:錯誤: document.all has no properties ,而第7種方法依舊返回“undefined
”。
小 結(jié)
name原來是為了標識之用,但是現(xiàn)在根據(jù)規(guī)范,都建議用id來標識元素。
以下只能用name:
1. 表單(form)的控件名,提交的數(shù)據(jù)都用控件的name而不是id來控制。因為有許多name會同時對應(yīng)多個控件,比如
checkbox和radio,而id必須是全文檔中唯一的。此外瀏覽器會根據(jù)name來設(shè)定發(fā)送到服務(wù)器的request。因此如果用id,服務(wù)器是無
法得到數(shù)據(jù)的。
2. frame和window的名字,用于在其他frame或window指定target。
以下只能用id:
1. label與form控件的關(guān)聯(lián),
<label for="MyInput">My Input</label>
<input id="MyInput" type="text">
for屬性指定與label關(guān)聯(lián)的元素的id,不可用name替代。
2. CSS的元素選擇機制,以#MyId的方式指定應(yīng)用樣式的元素,不能用name替代。
3. 腳本中獲得對象:
IE支持在腳本中直接以id(而不是name)引用該id標識的對象。例如上面的input,要在腳本中獲得輸入的內(nèi)容,可以直接以
MyInput.value來獲得。
如果用DOM的話,則用document.getElementById("MyInput").value,如果要用name的話,通常先得到包含控件的form,例如
document.forms[0],然后從form再引用name,注意這樣得到的是經(jīng)過計算后將發(fā)送給服務(wù)器的值。
name與id的其他區(qū)別是:
id要符合標識的要求,比如大小寫敏感,最好不要包含下劃線(因為不兼容CSS)。而name基本上沒有什么要求,甚至可以用數(shù)字
。
用CSS控制這個鏈接的停留樣式,
可以這樣寫 #m_blog div.opt a:hover{color:#D57813} 或 #myLink:hover{color:#D57813}
NAME主要應(yīng)用在交互式網(wǎng)頁,表單提交給某個服務(wù)器端腳本后接收變處理量使用。從源代碼的規(guī)范性和兼容性角度出發(fā),如在客戶端
腳本里要索引某個對象,建議用document.getElementById()方法,盡量不要直接使用NAME的值,當(dāng)然如果不考慮兼容性,以上9種方
法都可以在IE里運行通過(IE5.0沒測試過)。
附:測試源代碼
<!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></title>
</head>
<body>
<form method="post" action="" name="demoform">
<input type="text" name="oDemo" value="DEMO" id="oDemo2" /><br />
<input type="button" value="oDemo" onclick="alert(oDemo.value)" /><br />
<input type="button" value="demoform.oDemo" onclick="alert(demoform.oDemo.value)" /><br />
<input type="button" value="document.all.oDemo" onclick="alert(document.all.oDemo.value)" /><br />
<input type="button" value="document.all.demoform.oDemo" onclick="alert(document.all.demoform.oDemo.value)" /><br />
<input type="button" value="document.forms[0].oDemo" onclick="alert(document.forms[0].oDemo.value)" /><br />
<input type="button" value="document.forms['demoform'].oDemo" onclick="alert(document.forms['demoform'].oDemo.value)" /><br />
<input type="button" value="document.forms['demoform'].childNodes[0]" onclick="alert(document.forms
['demoform'].childNodes[0].value)" /><br />
<input type="button" value="document.forms['demoform'].elements[0]" onclick="alert(document.forms
['demoform'].elements[0].value)" /><br />
<input type="button" value="document.getElementById('oDemo2')" onclick="alert(document.getElementById('oDemo2').value)" /><br />
</form>
</body>
</html>
相關(guān)文章
動態(tài)更改網(wǎng)頁HTML元素(對象)內(nèi)容
動態(tài)更改網(wǎng)頁HTML元素(對象)內(nèi)容...2006-11-11css創(chuàng)意ul+li實現(xiàn)的細線表格實現(xiàn)代碼
非常用創(chuàng)意的用ul+li實現(xiàn)的細線表格效果,不用table2008-08-08