简体   繁体   English

如何将DOM状态重置为初始状态(首次接收页面时?)

[英]How to reset DOM state to the initial state (when page was firstly received?)

I manipulated a webpage DOM via some js libraries which altered the behaviour of onMouseup onMousedown and onMousemove , among other things which I can't be sure of. 我通过一些js库操作了网页DOM,这些库改变了onMouseup onMousedownonMousemove的行为,但我不确定。

Using a DOM inspector I could see that some of these changes are set as properties of the main "document" object. 使用DOM检查器,我可以看到其中一些更改被设置为主要 “文档”对象的属性。

So, what I am trying to figure out is a way to store, when the page is loaded, the initial state of the "document" object (or probably store the entire DOM?) so that later I will be able to restore it. 因此,我试图找出的是一种存储方法,该方法是在页面加载时存储“文档”对象的初始状态(或者可能存储整个DOM?),以便以后可以恢复它。

My naive attempt at this: 我对此的幼稚尝试:

var initial_state = document
document = initial_state

Guess what? 你猜怎么了? It didn't work... So... what do I need to know and how could I do it right? 它没有用...所以...我需要知道什么,我该怎么做呢?

UPDATE : I am aware now that java assignment is by reference and not by values so now I am now experimenting with .extend(), but the main point of this question is on wheter or not, by any means, I will be able to save the full state of the DOM at its initial stage (when page is received) and then restore it at a second time. 更新 :我现在知道java赋值是通过引用而不是通过值,所以现在我正在尝试.extend(),但是这个问题的重点是无论如何,无论如何,我将能够将DOM的完整状态保存在其初始阶段(收到页面时),然后再次进行恢复。 By full state I mean html, javascript state, object properties and methods so that when you restore it, the DOM is exactly the same as the one you received first time. 完整状态是指html,javascript状态,对象属性和方法,以便在还原它时,DOM与您第一次收到的DOM完全相同。

I understand that this can be difficult and can require case specific code directly proportional to the complexity of the javascript code state . 我知道这可能很困难,并且可能需要与javascript代码状态的复杂度成正比的特定案例代码 But it would help and I would be extremely grateful if I could see at least a code example for doing it the right way in the simplest of the cases (as in my question example case, where you receive some html and just some js that changes default values for onMousedown for the whole document). 但这会有所帮助,如果能在最简单的情况下(至少在我的问题示例中,您收到一些html以及一些发生变化的js代码)能看到至少一个以正确的方式完成此操作的代码示例,我将非常感激整个文档的onMousedown的默认值)。

Because how JavaScript works (objects are not copied but passed by reference) initial_state var has just stored the reference to the DOM document. 因为JavaScript的工作方式(不复制对象,而是通过引用传递对象),所以initial_state var刚刚存储了对DOM文档的引用。 When the DOM document is updated your initial_state var will reflect those changes because it's just a reference which is just pointing at the DOM document, and not an object "on its own". 当DOM文档更新时,您的initial_state var将反映这些更改,因为它只是指向DOM文档的引用,而不是“独立的”对象。

To obtain a copy of an object which is a "real" object and not just a reference to the first object, you need to clone . 要获得对象的副本,该对象是“真实”对象,而不仅仅是对第一个对象的引用,您需要clone

If you use jQuery you can clone by using extend function 如果您使用jQuery,则可以使用扩展功能进行克隆

// for simple cloning
var initDoc = $.extend({}, document);

// for deep cloning
var initDocument = $.extend(true, {}, document);

If you are not interested in using jQuery.extend() please take a look this related question on cloning object in JavaScript. 如果您对使用jQuery.extend()不感兴趣,请查看有关在JavaScript中克隆对象的相关问题。

How do I correctly clone a JavaScript object? 如何正确克隆JavaScript对象?

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

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