经过一系列操作后,如何将元素重置为原始状态?

发布于 2024-09-03 10:50:53 字数 59 浏览 2 评论 0原文

经过一系列动画、添加类和设置 css 样式之后。有没有一种简单的方法可以将元素重置为原始服务器交付状态?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩一世无双 2024-09-10 10:50:53

jQuery 使用内联 style 属性设置其操作,因此只需将其设置为 '' 即可。

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

这假设来自服务器的元素上没有设置内联 style 属性。如果是这样,最好使用样式表。

如果该元素必须来自设置了 style 属性的服务器,那么我想您可以将该值缓存在变量中,并在需要时重置它。

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


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

编辑:

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

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

然后您可以随时参考原件。

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

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

或者查找 class 属性已从原始属性修改的元素。

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

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');
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文