gpt4 book ai didi

dom - IE9 : childNodes not updated after splitText?

转载 作者:行者123 更新时间:2023-12-04 21:55:14 25 4
gpt4 key购买 nike

在 Internet Explorer 9 上,调用 splitText在文本节点上不会更新 childNodes它的 parent 。在 Chrome 和 Firefox 上,正如 https://developer.mozilla.org/En/DOM/Text.splitText 所预期的那样.

然而 IE9 在 console.dir 时运行正常在文本节点父 (?) 上调用

例子:

<!DOCTYPE html>
<html lang="en">

<meta charset="utf-8">

<script type="text/javascript" charset="utf-8">

window.onload = function() {

var e = document.querySelector('#test p');

var f = e.childNodes[0].splitText(10);

console.log(e.childNodes.length)

// console.dir(e)

console.log(e.childNodes.length)

}

</script>

<div id="test">
<p>Senectus et netus et malesuada fames ac turpis egestas.</p>
</div>

</html>

IE9 输出:
LOG: 1
LOG: 1

Chrome 和 Firefox 都正确输出:
2
2

取消注释时 console.dir(e) ,现在 IE9 输出:
LOG: 1
LOG: [object HTMLParagraphElement] {}
LOG: 2

这是一个错误吗?如果是这样,是否有任何解决方法可以“刷新”并反射(reflect) childNodes 的实际状态? , 除了 console.dir ?

更新 目前似乎有效的是添加/删除节点,例如:
var t = document.createTextNode("");
e.appendChild(t);
e.removeChild(t);

最佳答案

您可以使用解决方法而不是 splitText() :

function insertAfter(node, precedingNode) {
var nextNode = precedingNode.nextSibling, parent = precedingNode.parentNode;
if (nextNode) {
parent.insertBefore(node, nextNode);
} else {
parent.appendChild(node);
}
return node;
}

// Note that we cannot use splitText() because it is bugridden in IE 9.
function splitDataNode(node, index) {
var newNode = node.cloneNode(false);
newNode.deleteData(0, index);
node.deleteData(index, node.length - index);
insertAfter(newNode, node);
return newNode;
}

关于dom - IE9 : childNodes not updated after splitText?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7378186/

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