简体   繁体   English

有没有办法删除由JavaScript / JQuery所做的所有CSS更改

[英]Is there a way to drop all CSS changes made by JavaScript/JQuery

Aside from doing property = blank like the example below, is there a way to drop all css changes made by a script without refreshing the page? 除了像下面的示例那样执行property = blank之外,还有没有办法删除脚本所做的所有CSS更改而不刷新页面? Something like re-loading the original stylesheet? 是否需要重新加载原始样式表?

$("#div".css({property1:"", property2:""});

I'm currently studying JavaScript and jQuery and I want to implement some sort of "reset" button to my page, which in some parts, color and size can be modified by the user. 我目前正在研究JavaScript和jQuery,我想在页面上实现某种“重置”按钮,在某些部分,颜色和大小可由用户修改。

Well, if all your original styles were loaded in a style sheet and all your changes were done via jquery (which applies the change directly to the style attribute), then one easy way to do it is: 好吧,如果所有原始样式都加载到样式表中,并且所有更改都是通过jquery(将更改直接应用于style属性)完成的,那么一种简单的方法是:

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

If you want to apply it to all things with such a modification then you could use the attribute selector: 如果要通过这样的修改将其应用于所有事物,则可以使用属性选择器:

$('[style]').attr('style','');

Like mentionned in previous answer, you can simply overwrite the style attribute with blank. 就像前面的答案中提到的那样,您可以简单地用空白覆盖样式属性。

But, you can also remove it: 但是,您也可以将其删除:

$("Your selector").removeAttr("style");

If your element does not contain any other inline styles set by other functions, you could just set the default styles in your CSS style sheet, and remove the "style" attribute to remove its inline properties added through JS: 如果您的元素不包含其他函数设置的任何其他内联样式,则可以在CSS样式表中设置默认样式,然后删除“ style”属性以删除其通过JS添加的内联属性:

Pure JS: 纯JS:

var styled = document.querySelectorAll("*[style]");
for(i=0; i<styled.length;i++) {
    styled[i].removeAttribute("style");
}

jQuery: jQuery的:

$("*[style]").removeAttr("style");

Explanation: 说明:

Both approaches will remove inline styles from all the elements in the document. 两种方法都将删除文档中所有元素的内联样式。

Demo: 演示:

Here's a JSfiddle as a demo. 这是一个JSfiddle作为演示。

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

相关问题 如何恢复javascript对css的更改 - How to revert changes to css made by javascript 有没有办法观察CSS本身进行的属性更改并通过jQuery / JS做出反应? - Is there a way to observe property changes made by CSS itself and react via jQuery/JS? Javascript / jQuery监视CSS更改 - Javascript/jQuery watch for CSS Changes jQuery .css()禁用通过媒体查询进行的高度更改? - Jquery .css() disabling height changes made with media queries? Laravel/FancyTree 中 Jquery 对 CSS 所做的更改仅反映在 function 的末尾 - Changes made by Jquery to CSS in Laravel/FancyTree is only reflected at the end of the function 有什么方法可以保存用户对HTML页面所做的所有更改? - Is there any way to save all changes a user has made to an HTML page? 如何在页面刷新时保留 javascript/jquery 对 DOM 所做的更改 - How to keep the changes made to DOM by javascript/jquery on page refresh 通过JQuery和JavaScript函数的提交按钮所做的更改不会持续 - Changes made by submit button via JQuery and JavaScript function do not last 有没有办法拦截JavaScript在Web浏览器中发出的所有网络请求? - Is there a way to intercept all network requests made by JavaScript in a web browser? aspx不会看到javascript所做的更改吗? - Will aspx not see changes made by javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM