gpt4 book ai didi

javascript - Firefox 的 textContent 与 Chrome 的 innerText 不匹配

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:38:15 25 4
gpt4 key购买 nike

由于 Firefox 没有 innerText,我使用 textContent 来检索文档正文的文本。但是,textContent 会返回正文中的 noscriptscript 标签内的所有内容(可能还有其他标签,我不太确定),这意味着 textContent 看起来会有所不同innerText 通常返回的内容。

Firefox 中是否有返回与 Chrome 的 innerText 函数相同的输出的等效函数?

最佳答案

编辑

包含过滤器以不获取某些元素的内容

它们是两个不同的属性 - 一个在 W3C DOM 3 Core 中定义, 另一个是 Microsoft proprietary property已被广泛复制但没有开放规范。

规范化这两者的最佳方法可能是不使用它们,而是使用 DOM 遍历例程来收集文本节点并创建字符串。对两个(所有)浏览器使用相同的例程。

// Get the text within an element
// Doesn't do any normalising, returns a string
// of text as found.
function getText(element) {
var text = [];
var self = arguments.callee;
var el, els = element.childNodes;
var excluded = {
'noscript': 'noscript',
'script' : 'script'
};

for (var i=0, iLen=els.length; i<iLen; i++) {
el = els[i];

// May need to add other node types here
if ( el.nodeType == 1 &&
!(el.tagName.toLowerCase() in excluded)) {
text.push(self(el));

// If working with XML, add nodeType 4 to get text from CDATA nodes
} else if (el.nodeType == 3) {

// Deal with extra whitespace and returns in text here.
text.push(el.data);
}
}
return text.join('');
}

关于javascript - Firefox 的 textContent 与 Chrome 的 innerText 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6272767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com