如何在 javascript 中将样式属性重置为其 CSS 默认值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3506050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:57:13  来源:igfitidea点击:

How to reset the style properties to their CSS defaults in javascript?

javascripthtmlcssdynamic-html

提问by Calmarius

On a php generated page there are several elements like this:

在 php 生成的页面上有几个这样的元素:

<td class="defaultTDStyle" style="color:userDefinedCustomColor" id="myTDId"></td>

So there is a default style and I apply several extra styles that override the style defined in the CSS.

所以有一个默认样式,我应用了几个额外的样式来覆盖 CSS 中定义的样式。

Is there a way to remove these added styles from javascript? It seems the obj.style.color="default" and obj.style.color="auto" not works. How can I reset the color to the CSS default from javascript?

有没有办法从javascript中删除这些添加的样式?似乎 obj.style.color="default" 和 obj.style.color="auto" 不起作用。如何将颜色从 javascript 重置为 CSS 默认值?

回答by Richard JP Le Guen

If recollection serves, obj.style.color=""should work... I don't know if it's rightthough.

如果回忆有用,obj.style.color=""应该有用……我不知道这是否正确

回答by Pointy

Set the style property values to the empty string:

将样式属性值设置为空字符串:

 obj.style.color = "";

回答by trusktr

The new way:

新方式:

el.attributeStyleMap.delete('color')

or clear everything:

或清除一切:

el.attributeStyleMap.clear()

Not all browsers support this yet though. See StylePropertyMapon MDN for more details and browser compatibility.

不过,并非所有浏览器都支持此功能。有关StylePropertyMap更多详细信息和浏览器兼容性,请参阅MDN。

See also CSS Typed Object Modeland Working with the new CSS Typed Object Model.

另请参阅CSS 类型对象模型使用新的 CSS 类型对象模型