[英]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添加的内联属性:
var styled = document.querySelectorAll("*[style]");
for(i=0; i<styled.length;i++) {
styled[i].removeAttribute("style");
}
$("*[style]").removeAttr("style");
Both approaches will remove inline styles from all the elements in the document. 两种方法都将删除文档中所有元素的内联样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.