如何将元素重置为原始状态,经过一系列的操作? [英] How to reset elements to their original state, after a chain of manipulations?

查看:267
本文介绍了如何将元素重置为原始状态,经过一系列的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过一堆动画,添加类和设置css样式。是否有一个简单的方法来重置一个元素回到其原始服务器交付状态?

解决方案

jQuery设置其操作使用inline style 属性,因此只需将其设置为''

  $('#someDiv')。attr('style',''); 

这假设没有inline style 属性设置在来自服务器的元素上。更好地使用样式表。



如果元素必须来自具有 style 属性集的服务器,我想你可以缓存在一个变量的值,并在需要时重置它。

  //缓存原始属性
var originalAttributes = $('#someDiv')。attr('style');


//从原来的
$('#someDiv')重置attr('style',originalAttributes);






EDIT: / p>

如果需要,您可以使用自定义属性从服务器发送您的元素,以记住原始的 class 属性,例如。

 < div class =myClassoriginalClass =myClass> ...< / div> 

然后,您可以随时参考原文。



您甚至可以找到具有 originalClass 属性的所有元素。

 code> var $ elementsWithOriginal = $('[originalClass]'); 

或查找 class 属性

  var $ modifiedFromOriginal = $('[originalClass]')filter(function b $ b return $(this).attr('class')!= $(this).attr('originalClass'); 
});


After a bunch of animating, adding classes and setting css styles. is there an easy way to reset an element to be back in its original server delivered state?

解决方案

jQuery sets its manipulations using the inline style attribute, so just set that to ''.

$('#someDiv').attr('style','');

This assumes there were no inline style attributes set on the element coming from the server. Better to use style sheets if so.

If the element must come from the server with style attributes set, then I suppose you could cache the value in a variable, and reset it when needed.

   // Cache original attributes
var originalAttributes = $('#someDiv').attr('style');


   // Reset from original
$('#someDiv').attr('style',originalAttributes);


EDIT:

If needed, you could send your elements from the server with custom attributes to remember the original class attribute, for example.

<div class="myClass" originalClass="myClass">...</div>

Then you can reference the original anytime you need.

You could even find all elements that have the originalClass attribute like this.

var $elementsWithOriginal = $('[originalClass]');

or find elements where the class attribute has been modified from the original.

var $modifiedFromOriginal = $('[originalClass]').filter(function() {
        return $(this).attr('class') != $(this).attr('originalClass');
    });

这篇关于如何将元素重置为原始状态,经过一系列的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆