认识 getAttribute() setAttribute()

getAttribute()方法不属于document对象,所以不能通过document对象调用,它只能通过元素节点对象调用

 

var paras = document.getElementsByTagName("p");
    for (var i = 0; i < paras.length; i++) {
        alert(paras[i].getAttribute("title"));
    }

 

var paras = document.getElementsByTagName("p");
    for (var i = 0; i < paras.length; i++) {
        var title_text=(paras[i].getAttribute("title"));
        if (title_text) {
            paras[i].setAttribute("title","你好");
            alert(paras[i].getAttribute("title"));
        }
    }

posted @ 2016-04-26 21:00  殷的博客-Exploration  阅读(132)  评论(0编辑  收藏  举报