且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从元素中删除所有继承和计算的样式?

更新时间:2023-12-05 10:31:58


是否有某种方法可以执行此操作

Is there a certain way to do this

如果选择器匹配,则无法停止应用于元素的规则

There is no way to stop rules applying to an element if the selector matches.

您可以


  • 设置每个要拥有特定值(使用适当的特定选择器和!important

  • 将元素移动到其他文档


  • 删除规则(因此不适用于更改规则集上的选择器(因此不再匹配元素,您必须小心确保仍然选择您关心的元素)

  • set every property you want to have a specific value (with a suitably specific selector and !important
  • move the element to a different document (which could then be loaded with, for example, an iframe).
  • remove the rule (and thus cause it to not apply to any element)
  • change the selector on the ruleset (so it doesn't match the element any more, you have to be careful to make sure it still selects elements you care about)

除了获取所有计算的样式列表并将它们设置为空以外吗?

other than getting the list of all computed styles and setting them empty?

这不会工作。大多数属性不接受空白值,因此规则将无效并被忽略(从而导致之前的规则再次应用)。

That wouldn't work. Most properties won't accept a blank value, so the rule would be invalid and ignored (thus causing the previous rule to apply again).