js总结33 :javascript-DOM节点属性

时间:2022-07-07 12:28:08

1 设置节点属性三个方法:

获取:getAttribute(名称)

设置:setAttribute(名称, 值)

删除:removeAttribute(名称)

举个例子:

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script> window.onload = function () {
var eleNode = document.getElementsByTagName("img")[0];
//1.元素节点.属性或者元素节点[属性]
eleNode.src = "image/aaa.png";
eleNode.aaa = "bbb";
console.log(eleNode.aaa);
console.log(eleNode.src);
console.log(eleNode.tagName);
console.log(eleNode["title"]);
console.log(eleNode["className"]);
console.log(eleNode["alt"]); //2.元素节点.方法();
console.log(eleNode.getAttribute("id"));
eleNode.setAttribute("id","你好");
eleNode.setAttribute("ccc","ddd"); eleNode.removeAttribute("id");
} </script>
</head>
<body>
<img src="data:image/aaa.png" class="box" title="图片" alt="aaa" id="aaa"/>
</body>
</html>