简体   繁体   English

Javascript:保存和还原样式状态在IE中不起作用?

[英]Javascript: saving and restoring style state not working in IE?

Suppose we store a style as follows: 假设我们存储如下样式:

var  CSSobject = obj;
var  origStyle = getComputedStyle(obj);

...then we perform a number of operations that manipulate and change the style of the object. ...然后我们执行许多操作,以操纵和更改对象的样式。 After we are done with the changes we finally do something like this to restore the state of the style before the changes: 完成更改后,我们最终将执行以下操作以恢复更改之前的样式状态:

CSSobject.style = origStyle;

The above technique works for Chrome and Firefox, but I notice it does not function in Edge 25.10 or Explorer. 上述技术适用于Chrome和Firefox,但我注意到它在Edge 25.10或Explorer中不起作用。 (Note: the objective was to restore an arbitrary state back to itself, and not to get back to defaults) (注意:目标是将任意状态恢复为自身状态,而不是恢复为默认值)

Is this a simple fix ? 这是一个简单的解决方法吗? or is it a can of worms ? 还是一罐蠕虫?

PS. PS。 I am not into Jquery (not using it). 我不喜欢Jquery(不使用它)。 Pure Javascript solutions are preferable at the moment. 目前最好使用纯Javascript解决方案。 Thanks ! 谢谢 !

If you are making all changes inline, I would suggest store the style attribute instead of the entire computed style. 如果要内联进行所有更改,则建议存储style属性,而不要存储整个计算的样式。 This way it would be more efficient. 这样,它将更有效率。

EDIT 编辑

http://codepen.io/jammer99/pen/JXpdMB This works for me in IE and EDGE http://codepen.io/jammer99/pen/JXpdMB这适用于IE和EDGE

var CSSobject = $("h1")[0];
var origStyle = window.getComputedStyle(CSSobject); //,null); passing null is optional or you can pass the pseudo element of which you want the properties of 
var switchStyle = function(to) {
  for (p in origStyle) {
    to.style[p] = origStyle[p];
  }
};
switchStyle($("span")[0])

I am not sure but you could snapshot current style on an object somethig like below code does. 我不确定,但是您可以像下面的代码那样在对象somethig上捕获当前样式。 It's just and idea. 这只是想法。 (ref http://blog.stevensanderson.com/2013/03/15/animating-lists-with-css-3-transitions/ ) (请参阅http://blog.stevensanderson.com/2013/03/15/animating-lists-with-css-3-transitions/

    /*Snapshotting utils*/
    (function () {
        var stylesToSnapshot = ["transform", "-webkit-transform"];

        $.fn.snapshotStyles = function () {
            if (window.getComputedStyle) {
                $(this).each(function () {
                    for (var i = 0; i < stylesToSnapshot.length; i++)
                        this.style[stylesToSnapshot[i]] = getComputedStyle(this)[stylesToSnapshot[i]];
                });
            }
            return this;
        };

        $.fn.releaseSnapshot = function () {
            $(this).each(function () {
                this.offsetHeight; // Force position to be recomputed before transition starts
                for (var i = 0; i < stylesToSnapshot.length; i++)
                    this.style[stylesToSnapshot[i]] = "";
            });
        };
    })();

    /*example usage*/
    $(ui.item).snapshotStyles().removeClass("dragging").releaseSnapshot();

It would be interesting to see a working page and exactly the problem you are facing. 看到一个工作页面以及您正面临的问题将很有趣。 Since getComputedStyle() works in all browsers, I can only assume that your problems stem from the assignment of the computed value to the element's style. 由于getComputedStyle()在所有浏览器中均可使用,因此我只能假设您的问题源于将计算值分配给元素的样式。

I have to say that just assigning the style to an object does not restore the state of the object as it was, it only tries to force it. 我不得不说,仅将样式分配给对象并不能像原来那样恢复对象的状态,而只是试图强制它。 For example a CSS rule with !important can override even an inline style rule. 例如,带有!important的CSS规则甚至可以覆盖内联样式规则。 Values in the computed style are computed from CSS and the state of the window, like width, for example. 计算样式中的值是根据CSS和窗口的状态(例如宽度)来计算的。 You set that in the style and your element is now frozen, unresponsive to changes in the size of the window, for example. 您在样式中进行设置,并且元素现在被冻结,例如,对窗口大小的更改无响应。

My suggestion is to find a harder to implement but better working solution for your real issue instead of using getComputedStyle. 我的建议是为您的实际问题找到一个更难实现但更有效的解决方案,而不是使用getComputedStyle。 Using the style value would make more sense, for example. 例如,使用样式值会更有意义。

What can I say, it is possible, but u can do it in a different way. 我能说什么,这是可能的,但您可以通过其他方式来做到。

U can't store and restore styles directly by the way u are trying. U无法以您尝试的方式直接存储和还原样式。

What u can do is store all the styles in the page (default styles) in a JS object one by one like this way with a little help from jQUery - 您可以做的就是,在jQUery的帮助下,将所有样式(默认样式)一个接一个地存储在JS对象中,就像这样-

var defaultStyle;
defaultStyle.background_color = [];
defaultStyle.background_color.p_classSelected = $("p.class_selected").css("background-color");

This example is given for only one element, you should do it for each and every style which will be changed in run-time . 此示例仅提供一个元素, 您应该为将在运行时更改的每种样式都进行此操作

Then you should create a function for restoring the styles from stored values in JS file like this way- 然后,您应该创建一个用于从JS文件中存储的值还原样式的函数,如下所示:

$("p.class_selected").css("propertyname",defaultStyle.background_color.p_classSelected);

This should be done every element u have changed style in run-time. 您应该在运行时更改样式的每个元素都应执行此操作。

More can be found here and here . 这里这里可以找到更多。


Update- 更新-

U can also store your design in Local Storage from where u will restore designs. U还可以将您的设计存储本地存储中 ,您可以从中恢复设计。

More can be found here . 这里可以找到更多。


Think u have your answer :). 想你有你的答案:)。

Alright, this was much easier than I thought (especially in my case where I use no inline styles at all; it is all external CSS). 好了,这比我想象的要容易得多(特别是在我根本不使用任何内联样式的情况下;这都是外部CSS)。

Anytime Javascript assigns a value via the .style selector it is essentially creating an internal style and assigns to it. .style Javascript通过.style选择器分配值时,它实际上是在创建内部样式并将其分配给它。

All I had to do is set the style to null and it is all back to reading the external CSS as if nothing happened el.style = null ... to make it a little more robust (in the off change that there is some minor internal style setting) I could save it as var origInlineStyle = el.style.cssText beforehand and then do el.style.cssText = origInlineStyle 我要做的就是将样式设置为null,这一切都回到了读取外部CSS的感觉,好像什么也没发生el.style = null ...使它更加健壮(在更改中,存在一些小问题)内部样式设置),我可以预先将其保存为var origInlineStyle = el.style.cssText ,然后再进行el.style.cssText = origInlineStyle

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

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