js中的attribute詳解

Attribute是屬性的意思,文章僅對部分兼容IE和FF的Attribute相關的介紹。
attributes:獲取一個屬性做爲對象
getAttribute:獲取某一個屬性的值
setAttribute:創建一個屬性,並同時給屬性捆綁一個值
createAttribute:僅創建一個屬性
removeAttribute:刪除一個屬性
getAttributeNode:獲取一個節點做爲對象
setAttributeNode:創建一個節點
removeAttributeNode:刪除一個節點
attributes能夠獲取一個對象中的一個屬性,而且做爲對象來調用,注意在這裏要使用「[]」,IE在這裏能夠使用「()」,考慮到兼容性的問題,要使用「[]」。關於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四兄弟的概念比較容易理解,使用方法也比較簡單,惟一須要注意這幾點:
一、createAttribute在使用的時候不須要基於對象的,document.createAttribute()就能夠。
二、setAttribute,createAttribute在使用的時候若是是使用的時候不要使用name,type,value等單詞,IE都FF的反應都奇怪的難以理解。
三、createAttribute在使用的時候若是隻定義了名字,沒有d.nodeValue =
"hello";語句定義值,FF會認爲是一個空字符串,IE認爲是undefined,注意到這點就能夠了。 node

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> spa


setAttribute的使用方法:(你會發現多了一個名爲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> ip


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

<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三個方法的特色是都直接操做一個node(節點),removeAttributeNode在一開始的時候總會用錯,可是充分理解了node的含義的時候,就可以應用自如了。 字符串

getAttributeNode的使用方法:
<body>
<div id = "t"><input type = "hidden" id = "sss"
value = "aaa"></div>
</body>
<script>
var d =
document.getElementById("sss").getAttributeNode("value");
document.write(d.name);
document.write(d.value);
//顯示 value
aaa
</script> get

setAttributeNode的使用方法:
<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> input

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> it


更多的關於attributes屬必性問題,可在w3school中查詢!
相關文章
相關標籤/搜索