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中查询!
相关文章
相关标签/搜索