国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

javascript中setAttribute兼容性用法分析

2019-11-19 18:33:05
字體:
供稿:網(wǎng)友

本文實(shí)例分析了javascript中setAttribute兼容性用法。分享給大家供大家參考,具體如下:

1:常規(guī)屬性建議使用 node.XXXX。
2:自定義屬性建議使用node.getAttribute("XXXX")。
3:當(dāng)獲取的目標(biāo)是JS里的關(guān)鍵字時建議使用node.getAttribute("XXX"),如label中的for。
4:當(dāng)獲取的目標(biāo)是保留字,如:class,請使用className代替。

setAttribute(string name, string value):增加一個指定名稱和值的新屬性,或者把一個現(xiàn)有的屬性設(shè)定為指定的值。

1、樣式問題

setAttribute("class", value)中class是指改變"class"這個屬性,所以要帶引號。

vName代表對樣式賦值。

例如:

代碼如下:

var input = document.createElement("input");input.setAttribute("type", "text");input.setAttribute("name", "q");input.setAttribute("class",bordercss);

輸出時:<input type="text" name="q" class="bordercss">,即,input控件具有bordercss樣式屬性

注意:class屬性在W3C DOM中扮演著很重要的角色,但由于瀏覽器差異性仍然存在。

使用setAttribute("class", vName)語句動態(tài)設(shè)置Element的class屬性在firefox中是行的通的,但在IE中卻不行。因?yàn)槭褂肐E內(nèi)核的瀏覽器不認(rèn)識"class",要改用"className";

同樣,firefox 也不認(rèn)識"className"。所以常用的方法是二者兼?zhèn)洌?/p>

代碼如下:

element.setAttribute("class", value); //for firefoxelement.setAttribute("className", value); //for IE

2、方法屬性等問題

例如:

代碼如下:

var bar = document.getElementById("testbt");bar.setAttribute("onclick", "javascript:alert('This is a test!');");

這里利用setAttribute指定e的onclick屬性,簡單,很好理解。

但是IE不支持,IE并不是不支持setAttribute這個函數(shù),而是不支持用setAttribute設(shè)置某些屬性,例如對象屬性、集合屬性、事件屬性,也就是說用setAttribute設(shè)置style和onclick這些屬性在IE中是行不通的。

為達(dá)到兼容各種瀏覽器的效果,可以用點(diǎn)符號法來設(shè)置Element的對象屬性、集合屬性和事件屬性。

代碼如下:

document.getElementById("testbt").className = "bordercss";document.getElementById("testbt").style.cssText = "color: #00f;";document.getElementById("testbt").style.color = "#00f";document.getElementById("testbt").onclick= function () { alert("This is a test!"); }

示例:

<!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>kingwell</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><body><div id="idHeader" class="class-header" title="kingwell" status="1"></div><label id="forUserName" for="userName" title="kingwell" status="1"></label><script type="text/javascript">var el = document.getElementById("idHeader");alert(el.getAttribute("id"));alert(el.id);// IE Firfox->idHeaderalert(el.getAttribute("class"));//IE6,IE7 -> null IE8,IE9,Firefox ->class-headeralert(el.class);//IE6,IE7,IE8->報(bào)錯 IE9,Firefox->undefinedalert(el.getAttribute("className"));//IE6,IE7->class-header ; IE8,IE9,Firefox -> undefinedalert(el.className);//All -> class-headervar elfor = document.getElementById("forUserName");alert(elfor.getAttribute("for"));//IE6,IE7->undefined IE8,9,Firefox->forUseNamealert(elfor.for )//IE6,IE7報(bào)錯,其它為undefinedalert(elfor.title)//全部輸出kingwellalert(elfor.status);//IE6-8 -> 1 IE9,Firefox->undefinedalert(elfor.getAttribute("status"))//全部輸出 1</script></body></html>

更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《JavaScript常用函數(shù)技巧匯總》、《javascript面向?qū)ο笕腴T教程》、《JavaScript中json操作技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對大家JavaScript程序設(shè)計(jì)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 山西省| 顺义区| 吉林省| 安国市| 甘洛县| 区。| 交口县| 班戈县| 高雄市| 勐海县| 五莲县| 上蔡县| 新晃| 灵石县| 微山县| 汽车| 万年县| 仲巴县| 广州市| 江津市| 穆棱市| 岚皋县| 三河市| 拉萨市| 临朐县| 仪征市| 垦利县| 邻水| 剑阁县| 子洲县| 南部县| 阜南县| 汉中市| 湖口县| 丽江市| 凤翔县| 尉氏县| 图木舒克市| 前郭尔| 海伦市| 德令哈市|