Home > Web Front-end > JS Tutorial > body text

Introduction to the use of JS.getTextContent(element,preformatted)_javascript skills

WBOY
Release: 2016-05-16 17:22:08
Original
1440 people have browsed it
复制代码 代码如下:

/*获取标签的文字*/
function getTextContent(element, preformatted) {
if (!elementIsVisible(element)) return '';
if (element.nodeType == 3 /*Node.TEXT_NODE*/) {
var text = element.data;
if (!preformatted) {
//text = text.replace(/n|r|t/g, " ");
text = normalizeNewlines(text);
}
return text;
}
if (element.nodeType == 1 /*Node.ELEMENT_NODE*/ && element.nodeName != 'SCRIPT') {
var childrenPreformatted = preformatted || (element.tagName == "PRE");
var text = "";
for (var i = 0; i < element.childNodes.length; i ) {
var child = element.childNodes.item(i);
text = getTextContent(child, childrenPreformatted);
}
// Handle block elements that introduce newlines
// -- From HTML spec:
//// "P | %heading; | %list; | %preformatted; | DL | DIV | NOSCRIPT |
// BLOCKQUOTE | F:wORM | HR | TABLE | FIELDSET | ADDRESS">
//
// TODO: should potentially introduce multiple newlines to separate blocks
if (element.tagName == "P" || element.tagName == "TR" || element.tagName == "BR" || element.tagName == "HR" || element.tagName == "DIV") {
text = "n";
}
return text;
}
return '';
}

/*元素是否可见*/
function elementIsVisible(element)
{
if(element.style.visiablity == "hidden" || element.style.display == "none")
return false;
else
return true;
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!