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

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="image/aaa.png" class="box" title="图片" alt="aaa" id="aaa"/>
</body>
</html>

 

posted @ 2018-04-26 15:45  前端邢晋宇  阅读(137)  评论(0编辑  收藏  举报