【问题标题】:How to remove style css of div inside the another div?如何在另一个 div 中删除 div 的样式 css?
【发布时间】:2016-10-01 22:56:36
【问题描述】:

我的 css 有点问题。

我有一个带有 css 的,它是由另一个 .我无法更改 css 文件。

我需要删除这种样式。有没有可能?

<div class="1">

   #Generated by code
   <div class="2" style="xxxxxxxx">
      This text is bold now. Cause of the style. I need normal!
   </div>
   #Generated by code

</div>

有什么我可以做的吗??

谢谢你:)

【问题讨论】:

  • 您可以在内部或外部添加自定义样式吗?如果是这样,您总是可以通过在规则结尾声明 !important 声明广告来过度限定内联样式规则;例如; .1 .2 {font-weight: normal !important;}

标签: html css styles


【解决方案1】:

为什么不使用javascript?

<body onload="myFunction()">

    <div class="1">

       #Generated by code
       <div class="2" style="xxxxxxxx">
          This text is bold now. Cause of the style. I need normal!
       </div>
       #Generated by code

    </div>

    <script>

function MyFunction 
{
   document.getElementsByClassName("2").style.blabla;
}

    </script>

甚至使用

<style>
.2
{
width:50px;
}
</style>

?

【讨论】:

    【解决方案2】:

    使用前面提到的 JavaScript 是实现所需行为的好方法。但是根据MDN

    document.getElementsByClassName()
    

    返回一个类似数组的结构。所以正确的语法是

    document.getElementsByClassName('2')[0].style.fontWeight = 'normal';
    

    或完全删除样式属性使用

    document.getElementsByClassName('2')[0].removeAttribute('style');
    

    【讨论】:

      【解决方案3】:

      您可以创建另一个 CSS 文件并尝试将更高的 specificity 应用于应用于您的 div 的样式。如果您将ID 设置为div 并应用新CSS 样式表中的样式,则它具有最高的specificity

      尽量避免使用!important exception,如果您可以为该div 为您的新CSS 样式设置更高的特异性。使用!important exception 被认为是一种不好的做法,因为他们在网页中放置了:

      使用 !important 是不好的做法,应该避免使用,因为它会破坏样式表中的自然级联,从而使调试变得更加困难。

      问题在于内联样式会覆盖外部样式表的所有样式,因此您可能必须在此处使用!important exception

      添加到元素的内联样式(例如 style="font-weight:bold")总是会覆盖外部样式表中的任何样式,因此可以认为具有最高的特异性。

      【讨论】:

        【解决方案4】:

        您可以为此使用!important

        <div class="_1">
            #Generated by code
            <div class="_2" style="font-weight: normal !important;">
                This text is bold now. Cause of the style. I need normal!
            </div>
            #Generated by code
        </div>
        

        或者您可以创建一个新的 CSS 文件,将其包含到您的模板中。在您的 CSS 中,您可以输入以下内容:

        ._1 > ._2 {
            font-weight: normal !important;
        }
        

        请注意,标识符不能以数字开头,因此您必须在其后面加上一个字母或下划线,例如 CSS 中的 _1 会是 ._1 { }。阅读这篇文章:Allowed characters for CSS identifiers

        根据下面@Error404 的评论:!important 内联样式不需要异常。正如 MDN 所说,添加到元素的内联样式(例如 style="font-weight:bold")总是会覆盖外部样式表中的任何样式,因此可以被认为具有最高的特异性。

        阅读更多:https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity#The_!important_exception@Error404 也提供了一个答案,其中包含有关此问题的更多详细信息(在此答案的下方或上方)

        查看工作小提琴:https://jsfiddle.net/o24jww1b/1/

        【讨论】:

        • 我无法更改第二个 div。我只能更改第一个。
        • !important exception 不是内联样式所必需的。正如MDN 所说,添加到元素的内联样式(例如,style="font-weight:bold")总是会覆盖外部样式表中的任何样式,因此可以被认为具有最高的特异性。
        • 谢谢,我已在我的回答@Error404 中附上了您的评论 :-)
        • @C0dekid 不客气 :) 但我认为您还应该添加指向 MDN 规范的链接以更好地澄清。
        • 所以,现在我参考了您的分析器,因为它包含有关这一切的扩展信息 :-) @Error404
        【解决方案5】:

        你可以用 jquery 做到这一点 用这个

        <script>
        $(document).ready(function(){
                $("._2").css("font-size", "normal");
        });
        </script>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-02-07
          • 1970-01-01
          • 2018-10-21
          • 1970-01-01
          • 1970-01-01
          • 2013-07-14
          • 2012-11-18
          • 1970-01-01
          相关资源
          最近更新 更多