JS Attribute屬性操作詳解
Attribute是屬性的意思,文章僅對部分兼容IE和FF的Attribute相關的介紹。
attributes:獲取一個屬性作為對象
getAttribute:獲取某一個屬性的值
setAttribute:建立一個屬性,并同時給屬性捆綁一個值
createAttribute:僅建立一個屬性
removeAttribute:刪除一個屬性
getAttributeNode:獲取一個節(jié)點作為對象
setAttributeNode:建立一個節(jié)點
removeAttributeNode:刪除一個節(jié)點
attributes可以獲取一個對象中的一個屬性,并且作為對象來調(diào)用,注意在這里要使用“[]”,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四兄弟的概念比較容易理解,使用方法也比較簡單,唯一需要注意這幾點:
1、createAttribute在使用的時候不需要基于對象的,document.createAttribute()就可以。
2、setAttribute,createAttribute在使用的時候不要使用name,type,value等單詞,IE和FF的反應都奇怪的難以理解。
3、createAttribute在使用的時候如果只定義了名字,沒有d.nodeValue = "hello";語句定義值,F(xiàn)F會認為是一個空字符串,IE認為是undefined,注意到這點就可以了。
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的空屬性)
<head> <meta charset="UTF-8"> <title></title> <script> window.onload = function (){ var oBox = document.getElementById('box'); alert( document.body.innerHTML ); oBox.setAttribute('value','name'); alert( document.body.innerHTML ); attr = document.createAttribute('hallo'); alert( document.body.innerHTML );/*同上*/ attr.nodeValue = 'world';/*對自定義屬性進行編輯*/ alert( document.body.innerHTML );/*同上*/ oBox.setAttributeNode(attr);/*對標簽插入自定義屬性*/ alert( document.body.innerHTML );/*改變*/ }; </script> </head> <body> <ul id="box"> </ul> </body>
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三個方法的特點是都直接操作一個node(節(jié)點),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>
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>
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屬性操作詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
網(wǎng)頁編輯器ckeditor和ckfinder配置步驟分享
ckeditor+ckfinder配置用法,現(xiàn)在ckeditor是互聯(lián)網(wǎng)上應用比較廣泛的網(wǎng)頁編輯器了,這里介紹下配置方法,需要的朋友可以參考下2012-05-05實現(xiàn)網(wǎng)頁頁面跳轉的幾種方法(meta標簽、js實現(xiàn)、php實現(xiàn))
今天總結了幾種頁面跳轉的方法,分別是用meta標簽實現(xiàn)、用javascript實現(xiàn)、用php實現(xiàn),下面就來一一分享一下吧。2014-05-05詳解JavaScript函數(shù)callee、call、apply的區(qū)別
這篇文章主要介紹了JavaScript函數(shù)callee、call、apply的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03