gpt4 book ai didi

javascript - IE9 IE 10 IE11 createNodeIterator - "Exception occurred."

转载 作者:行者123 更新时间:2023-12-05 03:08:15 26 4
gpt4 key购买 nike

我正在使用 createNodeIterator 在我的文档中查找单词。

实际上,我几乎使用了来自 MDN 和 MSDN 的示例代码:

这在 Chrome 和 Firefox 中按预期工作。
不幸的是,Internet Explorer 11 只是说 Exception occurred. 并在我调用 iterator.nextNode

时停止

Exception occurred.

Error Debugger

代码笔:https://codepen.io/anon/pen/PKmQEe?editors=1011

代码示例:

var iterator = document.createNodeIterator(document.body, NodeFilter.SHOW_TEXT, {
acceptNode: function acceptNode(node) {
if (node.parentNode.tagName.toLowerCase() === 'span') {
return NodeFilter.FILTER_REJECT;
}

return NodeFilter.FILTER_ACCEPT;
}
}, false);

var nextNode = void 0;
var nodes = [];
while (nextNode = iterator.nextNode()) {
if (nextNode.nodeValue.indexOf('o') > -1) {
nodes.push(nextNode);
}
}

console.log(nodes.length);
nodes.forEach(function (node) {
return node.nodeValue = node.nodeValue.replace('o', 'OO');
});
<p>Lorem</p>
<p>Ipsum</p>
<div><div><div><div><p>Etiam ipsum purus, elementum vitae congue id, scelerisque nec lectus. Sed at urna id nulla elementum rhoncus. Praesent aliquet turpis justo, blandit eleifend sem. In purus eros, commodo vel pretium non, blandit non elit. Mauris tristique velit in neque malesuada congue. Suspendisse luctus varius nisi quis dignissim. Proin magna urna, tristique et adipiscing ut, ultrices ut leo.</p></div><span>Dolor</span></div></div></div>

最佳答案

写在 createNodeIterator fails in IE9 when acceptNode is specified您不得为 IE9 - IE11 传递对象,而是直接传递函数:

var iterator = document.createNodeIterator(document.body, NodeFilter.SHOW_TEXT, function acceptNode(node) {
if (node.parentNode.tagName.toLowerCase() === 'span') {
return NodeFilter.FILTER_REJECT;
}

return NodeFilter.FILTER_ACCEPT;
}, false);

var nextNode = void 0;
var nodes = [];
while (nextNode = iterator.nextNode()) {
if (nextNode.nodeValue.indexOf('o') > -1) {
nodes.push(nextNode);
}
}

console.log(nodes.length);
nodes.forEach(function (node) {
return node.nodeValue = node.nodeValue.replace('o', 'OO');
});
<p>Lorem</p>
<p>Ipsum</p>
<div><div><div><div><p>Etiam ipsum purus, elementum vitae congue id, scelerisque nec lectus. Sed at urna id nulla elementum rhoncus. Praesent aliquet turpis justo, blandit eleifend sem. In purus eros, commodo vel pretium non, blandit non elit. Mauris tristique velit in neque malesuada congue. Suspendisse luctus varius nisi quis dignissim. Proin magna urna, tristique et adipiscing ut, ultrices ut leo.</p></div><span>Dolor</span></div></div></div>

关于javascript - IE9 IE 10 IE11 createNodeIterator - "Exception occurred.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45595349/

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