JavaScript中的Attribute相關內容介紹


Attribute是屬性的意思 文章僅對部分兼容IE和FF的Attribute相關的介紹。



attributes:獲取一個屬性作為對象

getAttribute:獲取某一個屬性的值
setAttribute:建立一個屬性,並同時給屬性捆綁一個值
createAttribute:僅建立一個屬性
removeAttribute:刪除一個屬性

getAttributeNode:獲取一個節點作為對象
setAttributeNode:建立一個節點
removeAttributeNode:刪除一個節點

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

attributes的使用方法:(IE和FF通用)





Html代碼
1.<body>
2.<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
3.</body>



Js代碼
1.<script>
2.var d = document.getElementById("sss").attributes["value"];
3.document.write(d.name);
4.document.write(d.value);
5.//顯示value aaa
6.</script>



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

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

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

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



getAttribute的使用方法:





Html代碼
1.<body>
2.<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
3.</body>



Js代碼
1. <script>
2.var d = document.getElementById("sss").getAttribute("value");
3.document.write(d);
4.//顯示 aaa
5.</script>


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





Html代碼
1.<body>
2.<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
3.</body>




Js代碼
1.<script>
2.var d = document.getElementById("sss").setAttribute("good","hello");
3.alert(document.getElementById("t").innerHTML)
4.</script>

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





Html代碼
1.<body>
2.<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
3.</body>




Js代碼
1.<script>
2.var d = document.createAttribute("good");
3.document.getElementById("sss").setAttributeNode(d);
4.alert(document.getElementById("t").innerHTML)
5.</script>

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





Html代碼
1.<body>
2.<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
3.</body>



Js代碼
1.<script>
2.var d = document.getElementById("sss").removeAttribute("value");
3.alert(document.getElementById("t").innerHTML)
4.</script>

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



getAttributeNode的使用方法:




Html代碼
1.<body>
2.<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
3.</body>






Js代碼
1.<script>
2.var d = document.getElementById("sss").getAttributeNode("value");
3.document.write(d.name);
4.document.write(d.value);
5.//顯示 value aaa
6.</script>



setAttributeNode的使用方法:





Html代碼
1.<body>
2.<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
3.</body>




Js代碼
1.<script>
2.var d = document.createAttribute("good");
3.document.getElementById("sss").setAttributeNode(d);
4.alert(document.getElementById("t").innerHTML);
5.</script>

removeAttributeNode的使用方法:





Html代碼
1.<body>
2.<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
3.</body>



Js代碼
1.<script>
2.var d = document.getElementById("sss").getAttributeNode("value")
3.document.getElementById("sss").removeAttributeNode(d);
4.alert(document.getElementById("t").innerHTML);
5.</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM