我正在做一些JavaScript中的TextNodes操作,并且我(不幸)需要支持IE6。 Node.normalize()崩溃了,我需要解决这个问题。我的第一个倾向是用其他DOM方法重新实现它。我将如何实现这一点?在IE6 Node.normalize()崩溃
回答
以下版本更短,效率比别人张贴在这里。这些改进包括:
- 到
node.childNodes
不重复呼叫和node.childNodes.length
- 没有创造额外的文本节点;相反,每个合并,保持第一现有文本节点,并使用其
appendData()
方法 - 较短
代码:
function normalize(node) {
var child = node.firstChild, nextChild;
while (child) {
if (child.nodeType == 3) {
while ((nextChild = child.nextSibling) && nextChild.nodeType == 3) {
child.appendData(nextChild.data);
node.removeChild(nextChild);
}
} else {
normalize(child);
}
child = child.nextSibling;
}
}
您需要递归查看当前节点的所有子节点。在考虑节点时,您可以删除任何空的文本节点并组合任何相邻的文本节点。
function myNormalize(node)
for each child node of node do
if child is not text
normalize(child)
else
if child node is empty
delete
continue
else
sibling = next node
while sibling exists and sibling is a text node
if sibling is empty
delete sibling
else
combine sibling with child
get next sibling
end
end
end
end
end
好的伪代码。你能不能把它翻译成JavaScript? – 2010-01-07 20:16:16
基于tvanfosson的伪代码,这里就是我想出了在javascript:
var ELEMENT_NODE = 1;
var TEXT_NODE = 3;
function normalize(node) {
for (i=0; i<node.childNodes.length; i++) {
var child = node.childNodes[i];
if (child.nodeType == ELEMENT_NODE) {
normalize(child);
continue;
}
if (child.nodeType != TEXT_NODE) { continue; }
var next = child.nextSibling;
if (next == null || next.nodeType != TEXT_NODE) { continue; }
var combined_text = child.nodeValue + next.nodeValue;
new_node = node.ownerDocument.createTextNode(combined_text);
node.insertBefore(new_node, child);
node.removeChild(child);
node.removeChild(next);
i -= 1;
}
}
我真的很讨厌接受我自己的回答,但我确实非常依赖tvanfosson来提出这个问题,而且我已经投票给他了。 – 2010-01-19 20:00:03
在溶液上方运行相当缓慢,崩溃的Firefox我。所以我优化了一下,现在它工作得很好(主要问题是反复引用HTML集合对象node.childNodes)。
感谢伟大的起点,但我想这是值得发帖:
function myNormalize(node) {
for (var i=0, children = node.childNodes, nodeCount = children.length; i<nodeCount; i++) {
var child = children[i];
if (child.nodeType == 1) {
myNormalize(child);
continue;
}
if (child.nodeType != 3) { continue; }
var next = child.nextSibling;
if (next == null || next.nodeType != 3) { continue; }
var combined_text = child.nodeValue + next.nodeValue;
new_node = node.ownerDocument.createTextNode(combined_text);
node.insertBefore(new_node, child);
node.removeChild(child);
node.removeChild(next);
i--;
nodeCount--;
}
}
我认为,以上所提供的解决方案是不完全正确的。 FWIW,这里是一个工作规范化功能再加上粘贴功能使用本机正常化,如果它是可用的:
function _myNormalizeNode(node) {
if (! node) {
return;
}
var ELEMENT_NODE = 1;
var TEXT_NODE = 3;
var child = node.firstChild;
while (child) {
if (child.nodeType == ELEMENT_NODE) {
this._myNormalizeNode(child);
}
else if (child.nodeType == TEXT_NODE) {
var next;
while ((next = child.nextSibling) && next.nodeType == TEXT_NODE) {
var value = next.nodeValue;
if (value != null && value.length) {
child.nodeValue = child.nodeValue + value;
}
node.removeChild(next);
}
}
child = child.nextSibling;
}
}
function _normalizeNode(node) {
if (! node) {
return;
}
if (typeof node.normalize == "function") {
return node.normalize();
}
return _myNormalizeNode(node);
}
如果答案不正确,我对答案的评论会有帮助。另外,你真正纠正了什么? – 2013-10-22 15:49:14
- 1. 这个插件如何设计使IE6崩溃,崩溃IE6?
- 2. IE6与此jQuery崩溃
- 3. dojo在使用Zend Framework时崩溃ie6
- 4. 使用HttpWebRequest和Css时IE6崩溃
- 5. 崩溃在BOXKeychainItemWrapper.m
- 6. IE6崩溃,同时显示简单的网站
- 7. jQuery 1.4.2 $ .ajax与指定的jsonpCallback崩溃IE6/7
- 8. Ajax工具包的问题CalendarExtender崩溃IE6
- 9. 崩溃
- 10. 崩溃
- 11. 崩溃
- 12. 崩溃
- 13. 崩溃
- 14. 崩溃
- 15. 崩溃
- 16. 崩溃
- 17. 崩溃
- 18. PushViewController现在崩溃
- 19. SystemUi崩溃在recents
- 20. 在NSMutableArray的崩溃
- 21. BIO_set_fp()崩溃在Windows
- 22. setRetainInstance在DialogFragment崩溃
- 23. dismissViewControllerAnimated在iOS8崩溃
- 24. Android崩溃在getSupportFragmentManager()
- 25. 崩溃
- 26. CoreLocation线程崩溃崩溃:com.apple.CoreLocation.ConnectionClient.0x16fcb870.events
- 27. 引导:崩溃组内崩溃组
- 28. 崩溃内崩溃不起作用
- 29. NSLayoutConstraints在ios7上崩溃而在ios8上崩溃
- 30. UIDocumentMenuViewController在iPad上崩溃,但在iPhone上不崩溃
+1'(不幸)' – SLaks 2010-01-07 20:04:48