getAttribute是一个函数,它只有一个参数,你打算查询的属性的名字:
object.getAttribute(attribute)
示例:
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset="utf-8"/>
<title>Shopping list</title>
<style type="text/css">
p{
color:yellow;
font-family: "arial",sans-serif;
font-size: 1.2em;
}
body{
color:white;
background-color: black;
}
#purchases{
border: 1px solid white;
background-color: #333;
color:#ccc;
padding: 1em;
}
</style>
</head>
<body>
<h1>What to buy</h1>
<p title="a gentle reminder">Don't forget to buy this stuff.</p>
<ul id = "purchases">
<li>A tin of beans</li>
<li class="sale">Cheese</li>
<li class="sale important">Milk</li>
</ul>
<script>
var paras = document.getElementsByTagName("p");
for(var i=0; i < paras.length; i++)
{
alert(paras[i].getAttribute("title"));
}
</script>
</body>
</html>
setAttribute()有点不同,它允许我们对属性节点的值做出修改。这里不能再举例。