【问题标题】:Remove CSS property added by insertRule()删除由 insertRule() 添加的 CSS 属性
【发布时间】:2021-03-09 05:48:12
【问题描述】:

我添加了带有insertRule 的CSS。现在我想在 1 秒后删除属性 background,使其保持之前的状态(由 Bootstrap 设置)。

const css = window.document.styleSheets[0];
css.insertRule(`
    .mt-preview .panel-heading {                
        background: yellowgreen !important;
        color: #fff !important;
        transition: all 0.75s;
    }
`, css.cssRules.length);

setTimeout(() => {
  document.querySelector('.mt-preview .panel-heading').style.removeProperty('background');
}, 1000);

我使用了removeProperty(),但这似乎不起作用。如何删除该属性?

Fiddle

【问题讨论】:

    标签: javascript twitter-bootstrap twitter-bootstrap-3


    【解决方案1】:

    您也可以使用 CSSStyleSheet 中的deleteRule,您只需要知道哪个索引是您要删除的规则。

    const css = window.document.styleSheets[0];
    css.insertRule(`
        .mt-preview .panel-heading {                
            background: yellowgreen !important;
            color: #fff !important;
            transition: all 0.75s;
        }
    `, css.cssRules.length);
    
    setTimeout(() => css.deleteRule(0), 1000) // use 10 in your JSFiddle
    <link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
    <div class="panel-group mt-preview">
      <div class="panel panel-success">
        <div class="panel-heading"><strong>My title</strong></div>
        <div class="panel-body">
          <div class="mt-preview-info">My text</div>
        </div>
      </div>
    </div>

    更新 - 基于 OP 评论

    好吧,但这会删除整个 CSS,但我只想删除属性背景。

    你可以从ElementCSSInlineStyle.style使用style,类似这样:

    const heading = document.querySelector('.mt-preview .panel-heading')
    
    heading.style = 'background: yellowgreen; color: #fff; transition: all 0.75s;'
    
    //or
    //heading.style.cssText = 'background: yellowgreen; color: #fff; transition: all 0.75s;'
    
    //or
    //heading.setAttribute('style', 'background: yellowgreen; color: #fff; transition: all 0.75s;')
    
    setTimeout(() => heading.style.removeProperty('background'), 1000);
    <link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" />
    <div class="panel-group mt-preview">
      <div class="panel panel-success">
        <div class="panel-heading"><strong>My title</strong></div>
        <div class="panel-body">
          <div class="mt-preview-info">My text</div>
        </div>
      </div>
    </div>

    【讨论】:

    • 好吧,但这会删除整个 CSS,但我只想删除属性 background
    • 好的,我知道在使用style 时如何删除它,但我只是想知道在使用insertRule() 时是否可以删除该属性,但还是谢谢。
    【解决方案2】:

    您可以使用deleteRule。每当您通过 insertRule 添加规则时,它将返回添加属性的索引。您可以使用相同的索引通过 deleteRule 删除属性。

    const css = window.document.styleSheets[0];
    const index = css.insertRule(`
        .mt-preview .panel-heading {                
            background: yellowgreen !important;
            color: #fff !important;
            transition: all 0.75s;
        }
    `, css.cssRules.length);
    
    setTimeout(() => {
     css.deleteRule(index)
    }, 1000);
    

    【讨论】:

    • 10 分钟后和我的答案一样,为什么?
    • 只是因为你没有解释规则,没有别的
    • 解释在我添加的链接中。无需添加相同的答案
    猜你喜欢
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2016-11-12
    • 2011-08-21
    • 2023-03-11
    相关资源
    最近更新 更多