getAttribute:取得属性;
setAttribute:设置属性;
代码
<!DOCTYPE >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script language="JavaScript">
function change() {
var input = document.getElementById("li1");
alert(input.getAttribute("title"));
input.setAttribute("title", "mgc");
alert(input.getAttribute("title"));
}
</script>
</head>
<body>
<ul id="u">
<li id="li1" title="hello">Magci</li>
<li>J2EE</li>
<li>Haha!</li>
</ul>
<input type="button" value="Change" onClick="change();" />
</body>
</html>
在下对setAttribute第一个参数的理解是:改变、添加标签的属性,如常见的:class、id、style、title等。
在这里补充一点:setAttribute可以设置自定义属性,也可以设置标签自带属性。
CSDN---补充:
1. setAttribute(attributename,attributename) 方法添加指定的属性,并为其赋指定的值。
属性可以是自定义的属性,如果这个指定的属性已存在,则仅设置/更改值
2. getArribute(attributename);获取某个属性的值;返回值为string类型
注:attributename,value都是字符串类型
3. attributes;返回元素属性的 NamedNodeMap(返回所有属性的集合,如果通过该方法获取属性,obj.attributes['attr'])。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。