DOM特性的JAVASCRIPT操作

来源:互联网 发布:java第三方支付原理 编辑:程序博客网 时间:2024/05/20 12:49

在作为XML和HTML文档的结构DOM中,特性作为ELEMENT的常用元素,作用非常大,如下面的元素说明:

<table width="436" border="1" id="myTable"> 其有特性width,border,id三个,那么在javascript中如何操作这些特性呢?DOM有几种方法可以实现。

(1)element.attributes["attName"].value

(2)element.attributes.getNameItem("attName").nodeValue,element.attributes.removeNameItem("attName")

 (3)element.getAttribute("attName") 获取,element.setAttribute("attName","attValue") 设置,element.removeAttribute("attName") 移除。

以上操作方法可用DOM操作中,但作为HTML DOM,其提供了特性作为属性操作的途径,所以可以用如下的方式进行操作:

element.attName=attValue

这种方法书写方面有更大的可读性。

原创粉丝点击