获取元素文本内容的通用函数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*获取元素文本内容的通用函数*/ | |
function text(e) { | |
var str = ""; | |
//判断元素是否包含子元素 | |
e = e.childNodes || e; | |
//遍历子元素,获取其文本内容 | |
for (var i = 0; i < e.length; i++) { | |
//如果子元素下面还包含子元素,则递归执行 | |
str += e[i].nodeType != 1 ? (e[i].nodeValue) : text(e[i].childNodes); | |
} | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
兼容IE6+ FF chrome