认识 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"));
        }
    }

原文地址:https://www.cnblogs.com/yinzf/p/5436610.html