element.cloneNode(true)(深度克隆)的效率如何?
我正在 XML DOM 对象中构建 HTML 代码,以用作使用 XSL 模板的 div 元素的 innerHTML 的内容。 传统上,我们创建一个新的 XML DOM 文档,并将输入参数添加为 XML 元素,以便通过 javascript 进行转换。 这非常耗时,因为我们基本上是从代表我们当前帐户的另一个 XML 文档中手动选取数据,并将数据复制到瞬态 XML DOM 文档中。
我想做的是克隆帐户文档的相关节点(即客户信息)并将其用作转换的基础。 我不想直接使用帐户文档,因为我希望能够添加转换特定输入,而不对帐户对象进行更改。
对于通常包含 2000 多个元素的文档中通常少于 200 个元素的所需节点,使用 .cloneNode(true) 的效率如何? 目标平台是IE6,没有外部工具(即ActiveX)。
I'm building the HTML code within an XML DOM object to be used as the contents of the innerHTML of a div element using an XSL template. Traditionally we create a new XML DOM document and add the input parameters as XML Elements for the transform via javascript. This is all very time-consuming as we are basically hand picking the data from another XML document that represents our current account and copying the data into a transient XML DOM document.
What I'd like to do is clone the relevant node of the account document (i.e. customer info) and use it as the basis for the transform. I don't want to use the account document directly as I'd like to be able to add transform specific input, without making changes to the account object.
How efficient is using .cloneNode(true) for a desired node of about typically less than 200 elements from a document of typically 2000+ elements? The target platform is IE6 with no external tools (i.e. ActiveX).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
IE 在某些事情上会失败。
例如,当您将副本添加到 DOM 时,选中的单选按钮/复选框将不会被选中。
示例:
http:// webbugtrack.blogspot.com/2008/03/bug-199-cant-clone-form-element-in-ie.html
http://webbugtrack.blogspot.com/2007/08/bug-242-setattribute-doesnt-always-work.html
要查看 IE 实际返回的内容,请尝试在其中一个页面的地址栏中用此替换 url,然后按 Enter 键。
javascript:'<xmp>'+window.document.body.outerHTML+'</xmp>';
如果您对结果感到满意,那就太棒了!,但我认为您最终会对 IE 返回的结果不满意(在 DOM 中,以及这个“字符串”值等效)。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
CloneNode 非常高效,但这样做会消耗更多内存。
另一种需要考虑的方法是使用模板对象和处理器,将附加/更改的数据作为参数传递给处理器以及原本克隆为输入元素的元素。 不过,这种方法需要对 XSL 进行相当重要的修改。
CloneNode is pretty efficient but it will be consuming more memory doing it that way.
Another approach to consider is to use a Template object and a processor, pass your additional/changed data as parameters to the processor and the element that you would have otherwise cloned as the input element. This approach would require fairly significant mods the XSL though.