JS.getTextContent(element,preformatted)使用介紹
更新時(shí)間:2013年09月21日 12:28:49 作者:
JS.getTextContent獲取標(biāo)簽的文字想必大家并不陌生吧,下面為大家介紹下具體的使用方法,感興趣的朋友可以參考下
復(fù)制代碼 代碼如下:
/*獲取標(biāo)簽的文字*/
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:
//<!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;
}
相關(guān)文章
淺談TypeScript 用 Webpack/ts-node 運(yùn)行的配置記錄
這篇文章主要介紹了淺談TypeScript 用 Webpack/ts-node 運(yùn)行的配置記錄,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Javascript表格翻頁效果的具體實(shí)現(xiàn)
表格翻頁的實(shí)現(xiàn)方式有很多,下面以js為例為大家詳細(xì)介紹下表格翻頁效果的具體實(shí)現(xiàn),感興趣的朋友可不要錯(cuò)過2013-10-10
Webpack打包過程中處理ES6模塊的循環(huán)依賴問題小結(jié)
Webpack通過“暫時(shí)性引用”特性處理ES6模塊的循環(huán)依賴,即在模塊加載時(shí)創(chuàng)建占位符,確保模塊能夠正確加載,本文介紹Webpack打包過程中如何處理ES6模塊的循環(huán)依賴,感興趣的朋友一起看看吧2025-02-02

