获取和设置属性

getAttribute:获得元素的属性
setAttribute:设置元素的属性

1、getAttribute
getAttribute是一个函数,他只包含一个参数–你打算查询的参数的名字。

1
object.getAttribute(attribute);

getAttribute不属于document对象,所以不能通过document对象调用,他只能通过元素节点对象调用
可以与getElementsByTagName方法合用

1
2
3
4
var paras = documents.getElementsByTagName("p");
for(var i=0;i<paras.length;i++){
alert(paras[i].getAttribute("title"));
}

2、setAttribute
setAttribute 也只能用于节点元素对象调用。

1
2
var shopping = document.getElementById("purchases");
shopping.setAttribute("title","a list of goods");