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

首頁 > 編程 > JavaScript > 正文

詳解JS中的attribute屬性

2019-11-19 16:44:10
字體:
供稿:網(wǎng)友

Attribute是屬性的意思,文章僅對部分兼容IE和FF的Attribute相關(guān)的介紹。

attributes:獲取一個屬性作為對象getAttribute:獲取某一個屬性的值

object.getAttributes(attribute) getAttribute方法不屬于document對象,所以不能通過document對象獲取,只能通過元素節(jié)點(diǎn)的調(diào)用。例如document.getElementsByTagName("p")[0].

getAttributes("title")

setAttribute:建立一個屬性,并同時給屬性捆綁一個值

允許對屬性節(jié)點(diǎn)做出修改,例如

var shoop=document.getElementsById("psdf');shoop.setAttribute("tittle","a lot of goods")

createAttribute:僅建立一個屬性
removeAttribute:刪除一個屬性
getAttributeNode:獲取一個節(jié)點(diǎn)作為對象
setAttributeNode:建立一個節(jié)點(diǎn)
removeAttributeNode:刪除一個節(jié)點(diǎn)

attributes可以獲取一個對象中的一個屬性,并且作為對象來調(diào)用,注意在這里要使用“[]”,IE在這里可以使用“()”,考慮到兼容性的問題,要使用“[]”。關(guān)于attributes屬性的使用方式上,IE和FF有巨大的分歧,在此不多介紹。attributes的使用方法:(IE和FF通用)

<body><div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div></body><script>   var d = document.getElementById("sss").attributes["value"];   document.write(d.name);document.write(d.value);//顯示value aaa</script>

getAttribute,setAttribute,createAttribute,removeAttribute四兄弟的概念比較容易理解,使用方法也比較簡單,唯一需要注意這幾點(diǎn):

1、createAttribute在使用的時候不需要基于對象的,document.createAttribute()就可以。

2、setAttribute,createAttribute在使用的時候如果是使用的時候不要使用name,type,value等單詞,IE都FF的反應(yīng)都奇怪的難以理解。

3、createAttribute在使用的時候如果只定義了名字,沒有d.nodeValue = "hello";語句定義值,F(xiàn)F會認(rèn)為是一個空字符串,IE認(rèn)為是undefined,注意到這點(diǎn)就可以了。

4/getAttribute的使用方法:

<body><div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div></body><script>var d = document.getElementById("sss").getAttribute("value");document.write(d);//顯示 aaa</script>

setAttribute的使用方法:(你會發(fā)現(xiàn)多了一個名為good的屬性hello)

<body><div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div></body><script>   var d = document.getElementById("sss").setAttribute("good","hello");   alert(document.getElementById("t").innerHTML)</script>

createAttribute的使用方法:(多了一個名為good的空屬性)

<body><div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div></body><script>   var d = document.createAttribute("good");   document.getElementById("sss").setAttributeNode(d);   alert(document.getElementById("t").innerHTML)</script>

removeAttribute的使用方法:(少了一個)

<body><div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div></body><script>   var d = document.getElementById("sss").removeAttribute("value");   alert(document.getElementById("t").innerHTML)</script>

getAttributeNode,setAttributeNode,removeAttributeNode三個方法的特點(diǎn)是都直接操作一個node(節(jié)點(diǎn)),removeAttributeNode在一開始的時候總會用錯,但是充分理解了node的含義的時候,就能夠應(yīng)用自如了。

getAttributeNode的使用方法:

<body><div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div></body><script>   var d = document.createAttribute("good");   document.getElementById("sss").setAttributeNode(d);   alert(document.getElementById("t").innerHTML);</script>

removeAttributeNode的使用方法:

<body><div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div></body><script>   var d = document.getElementById("sss").getAttributeNode("value")   document.getElementById("sss").removeAttributeNode(d);   alert(document.getElementById("t").innerHTML);</script>

以上所述是小編給大家介紹的JS中的attribute屬性,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 建宁县| 张北县| 定西市| 旅游| 宜黄县| 玉山县| 威宁| 翁源县| 大安市| 平度市| 辽宁省| 永德县| 吉林市| 吴桥县| 鄂尔多斯市| 阳新县| 确山县| 苏尼特左旗| 博爱县| 辽源市| 乐至县| 辽宁省| 陇西县| 丰镇市| 名山县| 双江| 乌海市| 驻马店市| 大名县| 南开区| 济宁市| 县级市| 井研县| 习水县| 潜江市| 五寨县| 博爱县| 茂名市| 澜沧| 扶沟县| 永城市|