簡體   English   中英

如何刪除重要的CSS屬性?

[英]How can you remove an important CSS property?

如果元素樣式屬性很重要(通過style=""或JS設置),如何刪除它?

removeProperty()不起作用( jsfiddle ):

elem.style.setProperty('background', '#faa', 'important');
elem.style.removeProperty('background'); // doesn't work

(最好是無框架解決方案,它只需要在Chrome中運行。)

您無法刪除該屬性的原因是因為它是一個速記屬性。

設置它時,實際上會添加其他屬性,但沒有“background”屬性,因此沒有要刪除的“background”屬性。

在這種情況下,您可以像這樣取消設置:

elem.style.removeProperty('background-color');

通常,您需要取消設置由速記屬性表示的每個“long-hand”屬性。


你也可以這樣做來覆蓋它:

elem.style.setProperty('background', 'inherit', 'important');

或者你可以像這樣核對元素的整個內聯樣式:

elem.style.cssText = '';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM