天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

document.getElementsByName和document.getElementById 在IE與FF中不同實現

對于ID & Name 按最經典的解釋的:“ID 就如同我們的身份證,Name就如同我們的名字”,也就是說,在一個html文檔中ID是唯一的,但是Name是可以重復的,就象我們的人名可以重復但是身份證確實全中國唯一的(PS:據說有重復的^_^)
但是對于document.getElementsByName 與document.getElementById 這個兩個方法,IE中是并沒有嚴格區分 ID 與 Name 的,比如:
<script type="text/Javascript">
function useGetElementsByNameWithId(id) {
var eles = document.getElementsByName('ID_A');
var msg = '使用 getElementsByName 傳入 ID:得到:'
if(eles.length > 0) {
msg += " Name " + eles[0].id;
}
alert(msg);
}
function usegetElementByIdWithName(name) {
var ele = document.getElementById(name);
var msg = '使用 getElementById 傳入 Name 得到:';
if(ele) {
msg += " ID " + ele.id;
}
alert(msg);
}
</script><input id="ID_A" name="Name_A" type="button" value="使用 getElementsByName 傳入 ID" onclick="useGetElementsByNameWithId(this.id)" />
<input id="ID_B" name="Name_B" type="button" value="使用 getElementsByName 傳入 Name" onclick="usegetElementByIdWithName(this.name)" />IE中通過 getId 傳入 name 同樣可以訪問到目標元素,而通過 getName 傳入 Id 也可以訪問到目標元素。
MSDN中對兩個方法的解釋:
getElementById Method
--------------------------------------------------------------------------------
Returns a reference to the first object with the specified value of the ID attribute.
Remarks
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.
MSDN確實對 getElementsByName 方法做了說明:“具有指定 Name 或者 ID 屬性的元素都會返回”,但是
getElementById 方法卻沒有說明,然而內部實現同 getElementsByName 是一樣的。
而對于FF,看來更忠實W3C標準,上面的測試代碼是沒有辦法返回目標元素的。
W3C 中的相關信息:
http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-26809268
由于有這個問題,所以對ASP.NET 控件中諸如 radiobuttonlist checkboxlist,使用客戶端腳本通過getElementsByName訪問具有name屬性的成組對象時就要注意了,因為radiobuttonlist 默認會呈現一個table來包容這些radio,而這個table id 與這些radio的name時相同的,比如:
.ASPx
<ASP:radiobuttonlist id="RadioButtonList1" runat="server">
<ASP:listitem>opt1</ASP:listitem>
<ASP:listitem>opt2</ASP:listitem>
<ASP:listitem>opt3</ASP:listitem>
</ASP:radiobuttonlist>HTML:
<table id="RadioButtonList1" border="0">
<tr>
<td><input id="RadioButtonList1_0" type="radio" name="RadioButtonList1" value="opt1" /><label for="RadioButtonList1_0">opt1</label></td>
</tr><tr>
<td><input id="RadioButtonList1_1" type="radio" name="RadioButtonList1" value="opt2" /><label for="RadioButtonList1_1">opt2</label></td>
</tr><tr>
<td><input id="RadioButtonList1_2" type="radio" name="RadioButtonList1" value="opt3" /><label for="RadioButtonList1_2">opt3</label></td>
</tr>
</table>
在IE中使用 document.getElementsByName('RadioButtonList1') 就是返回四個元素了,第一個元素是那個id為 RadioButtonList1 的table,
如果客戶端需要有這樣的script,也為代碼的跨瀏覽器帶來了的麻煩。
注:radiobuttonlist可以選擇“流布局”呈現,同樣會生成一個類似的外圍<span/>來做為容器,也會產生這個問題。

AspNet技術document.getElementsByName和document.getElementById 在IE與FF中不同實現,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 吃奶啃奶玩乳漫画 | 欧美在线看欧美视频免费 | 国产精品久久久久成人免费 | 亚洲成a人不卡在线观看 | 2021国产精品视频 | 99免费在线观看 | 88.7在线收听 | 伊人久久国产免费观看视频 | 99精品免费久久久久久久久蜜桃 | WWW国产亚洲精品久久麻豆 | 久久中文骚妇内射 | georgielyall装修工 | 亚洲欧美日韩精品自拍 | 大学生第一次破女在线观看 | 久久国内精品视频 | 曰产无码久久久久久精品 | 国产亚洲精品久久久久久无码网站 | 色宅男午夜电影网站 | 黑人玩弄极品人妻系列 | xnxnxn69日本| 国产午夜在线观看视频播放 | 99久久亚洲精品影院 | 亚洲精品午夜久久久伊人 | 狠狠狠色丁香婷婷综合久久 | 久久青草免费线观最新 | 日韩中文字幕欧美在线视频 | 女人麻豆国产香蕉久久精品 | 亚洲人成网站在线播放 | 日韩精品久久久久久久电影 | 青青久在线视频免费观看 | 日韩亚洲欧洲在线rrrr片 | 伊人久久精品线影院 | 亲嘴扒胸摸屁股视频免费网站 | 国产全部视频列表支持手机 | 99re久久热在线视频 | 一本大道香蕉中文在线视频观看 | 娇妻归来在线观看免费完整版电影 | 国产中文字幕乱码免费 | 国产中文欧美日韩在线 | 在线视频 国产 日韩 欧美 | 内射人妻骚骚骚 |