Skip to content

Instantly share code, notes, and snippets.

@hehongwei44
Created July 18, 2014 15:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save hehongwei44/08429d872a5f19ca7d6b to your computer and use it in GitHub Desktop.
获取元素文本内容的通用函数
/*获取元素文本内容的通用函数*/
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;
}
@hehongwei44
Copy link
Author

兼容IE6+ FF chrome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment