简体   繁体   English

替换javascript中的整个DOM

[英]Replace whole DOM in javascript

I need to implement undo-redo functionality in my project. 我需要在项目中实现撤消重做功能。 But it is very complex and every change affects on many elements. 但这非常复杂,每项更改都会影响许多要素。 I think saving and restoring whole page will be the best choice. 我认为保存和还原整个页面将是最佳选择。 But i have some problems with missing .data() params of DOM elements. 但是我有一些缺少DOM元素的.data()参数的问题。 I use next functions: 我使用下一个功能:

// save
var documentCopy = document.documentElement.cloneNode(true);

// restore
document.replaceChild(
documentCopy,
document.documentElement
);

How I can save and restore whole DOM with saving jQuery.data() of elements? 如何通过保存元素的jQuery.data()保存和还原整个DOM?

The trivial thing that I would try is using jQuery's clone instead. 我要尝试的琐碎事情是使用jQuery的clone Be sure to use it with two true parameters, but be careful as this may be very very slow. 请确保将其与两个true参数一起使用,但要小心,因为这可能非常慢。 Are you sure this is the only way to achieve what you want? 您确定这是实现所需目标的唯一方法吗? Can't you replace a smaller portion of the document? 您不能替换文档的一小部分吗?

Note that this doesn't seem to work well with document.documentElement , and that using it with the document's body seems to lose the data on the original elements (say what?). 请注意,这似乎无法与document.documentElement使用,并且与文档body似乎会丢失原始元素上的数据(说什么?)。 Here's a small test. 这是一个小测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM