Home  >  Article  >  Web Front-end  >  Javascript solves innerText browser compatibility problem code_javascript skills

Javascript solves innerText browser compatibility problem code_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:33:34903browse

The following function receives a parameter, which can be an array or an element, and returns the text of the element.

Copy code The code is as follows:

function text(e){
var str = "";
//If an element is passed in, get its child elements
//Otherwise, when it is an array
e=e.childNodes || e;
for ( var i = 0; i < e.length; i ) {
//Determine the element type
//If it is text, get its text, otherwise, traverse its child elements
str = e[ i].nodeType != 1 ? e[i].nodeValue : text(e[i].childNodes);
}
return str;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn