JS.getTextContent(element,preformatted)使用介绍

时间:2021-05-26

复制代码 代码如下:

function getTextContent(element, preformatted) {
if (!elementIsVisible(element)) return '';
if (element.nodeType == 3 ) {
var text = element.data;
if (!preformatted) {
//text = text.replace(/\n|\r|\t/g, " ");
text = normalizeNewlines(text);
}
return text;
}
if (element.nodeType == 1 && 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:
//<!ENTITY % block
// "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;
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章