gpt4 book ai didi

用于 IE 的 Javascript textContent(保留空格的 innerText)

转载 作者:行者123 更新时间:2023-11-30 06:03:26 25 4
gpt4 key购买 nike

我需要与 textContent 等效的 IE。

在您跳出裤子之前——innerText 与 textContent 不同。 innerText 在 DOM 渲染器解析时解析空格。 textContent 在写入时解析空格。我需要后者,但 I.E.不支持文本内容。

例如:

DOM Obj:   <p>This  is  an  example.</p>
innerText: "This is an example."
textContent: "This is an example."

最佳答案

我认为你做不到。 IE 甚至在 TextNode#nodeValue 属性中“标准化”空格。前一段时间我写过这个:

function textValue(element) { 
var collector = [];
textValueCollector(element, collector);
return collector.join("");
}
function textValueCollector(element, collector) {
var node;
for (node = element.firstChild; node; node = node.nextSibling)
{
switch (node.nodeType) {
case 3: // text
case 4: // cdata
collector.push(node.nodeValue);
break;
case 8: // comment
break;
case 1: // element
if (node.tagName == 'SCRIPT') {
break;
}
// FALL THROUGH TO DEFAULT
default:
// Descend
textValueCollector(node, collector);
break;
}
}
}

所以我认为这可能是您所需要的,但遗憾的是,不是。正如您在 this live example 中看到的那样,在 IE 上,即使我们直接遍历文本节点,额外的空格也会被忽略,所以它只显示 45 的长度,而实际上它是 61(在其他浏览器 [Chrome、Firefox、Opera] 上)。

关于用于 IE 的 Javascript textContent(保留空格的 innerText),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6826164/

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