获取/修改元素文本

/*
 一个参数,返回元素的textContent或者innertext
 两个参数,用value参数的值设置元素的textContent或者innerText
*/

function textContent(element, value) {
    var content = element.textContent; //检测textContent是否有定义
    if (value !== undefined) { //没有传递value,因此返回当前文本
        if (content !== undefined) {
            return content;
        } else {
            return element.innerText; //IE
        }
    } else { //传递了value,因此设置文本
        if (content !== undefined) { element.textContent = value };
        else element.innerText = value; //IE
    }
}




// 查找元素的后代中节点中的所有Text节点

//返回天涯不是有e的纯文本内容递归进入其子元素
//该方法类似与textContent属性

//CDATASection节点----它是Text的子类型,代表了CDATA段的内容
function getTextContent(argument) {
    var child, type, s = '';
    for (child = e.firstChild; child != null; child = child.nextSibling) {
        type = child.nodeType;
        if (type === 3 || type === 4) { //text和CDATASection节点
            s += child.nodeValue;
        } else if (type === 1) {
            s += getTextContent(child);//递归Element
        }
        return s;
    }
}
原文地址:https://www.cnblogs.com/superZz/p/5839503.html