如何删除重要的CSS属性? [英] How can you remove an important CSS property?
问题描述
如果元素样式属性很重要(设置为style=""
或JS槽),如何将其删除?
If an element style property is important (set either trough style=""
or JS), how can one remove it?
removeProperty()
不起作用( jsfiddle ):
elem.style.setProperty('background', '#faa', 'important');
elem.style.removeProperty('background'); // doesn't work
(最好是无框架解决方案,它只需要在Chrome中运行即可).
(Preferably a frameworkless solution, it only has to work in Chrome.)
推荐答案
之所以无法删除该属性,是因为它是简写属性.
The reason you can't remove the property is because it's a shorthand property.
设置后,实际上会添加其他属性,但是没有"background"属性,因此没有要删除的"background"属性.
When you set it, other properties actually get added, but no "background" property, so there's no "background" property to remove.
在这种情况下,您可以这样取消设置:
In this case, you can unset it like this:
elem.style.removeProperty('background-color');
通常,您需要取消设置由简写属性表示的每个长手"属性.
In general, you'd need to unset every "long-hand" property represented by the shorthand property.
您也可以这样做来覆盖它:
You could also do this to overwrite it:
elem.style.setProperty('background', 'inherit', 'important');
或者您可以像这样对元素的整个内联样式进行核对:
Or you could nuke the entire inline style for the element like this:
elem.style.cssText = '';
这篇关于如何删除重要的CSS属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!