亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript中為元素加上name屬性的方法

 更新時間:2011年05月09日 23:40:55   作者:  
干前端這行當(dāng)已經(jīng)超過一個月了, 每天都會遇到新奇古怪, 甚至離奇的問題. 雖然絕大部分都是一些小問題, 但我覺得還是有必要記錄下來
今天遇到個小問題, 在構(gòu)建 DOM 時, IE 中不能通過 element.setAttribute('name', _variable); 和 element.name = _variable; 這樣的形式來為元素加上 name 屬性, 無論是 IE6 還是 IE7. (IE8 是可以的, 但 IE8rc1 不行)

后來我查看了 MSDN, 得到信息如下:
復(fù)制代碼 代碼如下:

Internet Explorer 8 and later can set the NAME attribute at run time on elements dynamically created with the createElement method. To create an element with a NAME attribute in earlier versions of Internet Explorer, include the attribute and its value when using the createElement method.

也就是說, 我們必須通過帶屬性和值的標(biāo)簽來創(chuàng)建有 name 屬性的元素. 為求各瀏覽器兼容良好, 代碼可以這樣寫:
復(fù)制代碼 代碼如下:

var element = null;
try {
// IE6/IE7 構(gòu)建方式
element = document.createElement('<input name="radio-button">');
} catch (e) {
// W3C 構(gòu)建方式
element = document.createElement('input');
element.name = 'radio-button';
}
// 定義其他屬性
element.id = 'radio-1'
element.type = 'radio';

以前我是一個 Java 開發(fā)人員, 實(shí)際工作中 JavaScript 的代碼量其實(shí)不多, 在自己的一些小應(yīng)用中往往只是小打小鬧, 會 (潛意識地) 避開一些可能出問題的地方, 像跨域使用 AJAX, IE 內(nèi)存泄漏這樣的問題很少回去考慮. 但在轉(zhuǎn)做 UED 后, JavaScript 和交互應(yīng)用肯定會成為我以后的工作重點(diǎn), 發(fā)生大小各異問題的機(jī)會相當(dāng)多 (現(xiàn)在幾乎每天都有), 在解決各種問題的過程中, 我痛并快樂著. 本著不瞎搞, 少折騰的原則, 有必要把這些記錄一下, 為自己備份, 更能與人分享.

相關(guān)文章

最新評論