我想在页面中生成一个节点数组,其中将包含 Ranges 的所有可能 startContainers。
我尝试使用 treeWalker,但它给了我一个比实际 startContainer 节点更深的节点,例如:
<p>For those keeping count at home, by the way, the <a target="_blank" href="http://msdn.microsoft.com/en-US/windows/apps/br229516.aspx">Windows 8 Developer Preview site</a> still happily talks about "Metro style app development,"; even though <a target="_blank" href="http://www.istartedsomething.com/20120816/microsofts-new-rule-no-metro-named-apps/">rumor has it</a> that Microsoft is now banning all apps with the word "Metro" in their name from the Windows Store.</p>
(取自 techcrunch.com)
所以我的树行者返回以下内容:
['For those keeping count at home, by the way, the ','Windows 8 Developer Preview site',' still happily talks about "Metro style app development,"; even though ','rumor has it',' that Microsoft is now banning all apps with the word "Metro" in their name from the Windows Store.']
(分裂)
但是当我尝试得到以下内容时: window.getSelection().getRangeAt(0).startContainer.textContent 我得到:
['For those keeping count at home, by the way, the Windows 8 Developer Preview site still happily talks about "Metro style app development,"; even though rumor has it that Microsoft is now banning all apps with the word "Metro" in their name from the Windows Store.']
(不拆分)
为什么 startContainer 不更深(拆分)?像treeWalker是什么?
这是tree walker的代码:
var walker = document.createTreeWalker(document.body, NodeFilter.SHOW_ALL, function(node) {
if (node.nodeType == 3) {
return NodeFilter.FILTER_ACCEPT;
} else if (node.offsetWidth && node.offsetHeight) {
return NodeFilter.FILTER_SKIP;
} else {
return NodeFilter.FILTER_REJECT;
}
}, false);