简体   繁体   English

为什么不能在Bootstrap按钮上覆盖CSS背景颜色?

[英]Why cannot override CSS background color on Bootstrap button?

What I want to happen 我想要发生什么

在此输入图像描述

What's actually happening 实际发生了什么

在此输入图像描述

I want to toggle the red-background class, so that when the mouse hovers over the button-bullet , it changes its background-color to #FCC1C5 . 我想切换red-background类,这样当鼠标悬停在button-bullet ,它会将其background-color更改为#FCC1C5

I don't want to use .btn.button-bullet:hover because I have some jQuery logic that disables the remove button from showing up when there is only one bullet element on screen. 我不想使用.btn.button-bullet:hover因为我有一些jQuery逻辑,当屏幕上只有一个子弹元素时,它禁止显示删除按钮。

For some reason, when I toggleClass("red-background") , nothing shows up. 出于某种原因,当我toggleClass("red-background") ,没有任何显示。

I think it might be because in the CSS, I set .btn.button-bullet 's background-color to transparent . 我想这可能是因为在CSS中,我将.btn.button-bulletbackground-colortransparent So if that is the problem , how do I override the background-color in jQuery? 所以, 如果这是问题 ,我如何覆盖jQuery中的background-color

Code

HTML HTML

<div class="worksheet-problems">
  <div class="row">
    <div class="col-lg-7">
      <div class="form-group">
        <div class="input-group input-group-lg worksheet-problem">
          <div class="input-group-btn">
            <button type="button" class="btn btn-default button-bullet"> <span class="glyphicon glyphicon-one-fine-dot" aria-hidden="true"></span> </button>
          </div>
          <input type="text" name="Worksheet-Problem" class="form-control" placeholder="Problem..." aria-label="Write worksheet problem here">
          <div class="input-group-btn">
            <button type="button" class="btn btn-default button-add" aria-label="Add"> <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span> </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

CSS CSS

.red-background{
  background-color: #FCC1C5;
}
/*get rid of Boostrap default grey for buttons*/
.btn.button-add, .btn.button-bullet{
  background-color: transparent;
}

jQuery jQuery的

$(document).ready(function() {
  $(".worksheet-problems").on("mouseenter mouseleave", ".button-bullet", function() {
    if ($(".worksheet-problems").children().length > 1) {
      $(this).children(".glyphicon").toggleClass("glyphicon-one-fine-dot");
      $(this).toggleClass("red-background");
      $(this).children(".glyphicon").toggleClass("glyphicon-minus-sign");
    }
  });
});

JSFiddle 的jsfiddle

Problem: your red-background class has lower specificity than .btn.button-bullet . 问题:你的red-background类的特异性低于.btn.button-bullet

Solution (add more specificity and place it after selectors with transparent background): 解决方案(添加更多特异性并将其置于具有透明背景的选择器之后):

.btn.button-add, .btn.button-bullet {
  background-color: transparent;
}
.btn.red-background {
  background-color: #FCC1C5;
}

Looks like 看起来像

$(this).toggleClass("red-background"); 

ain't the background element. 不是背景元素。 So check which element you're actually altering: 因此,请检查您实际改变的元素:

console.log($(this)); 

to check that it actually is the right element you want to alter. 检查它实际上是你想要改变的正确元素。 And a hack sometimes, but a lifesafer can be to use : 有时候是黑客攻击,但可以使用一个生命周期:

.red-background{
    background-color: #FCC1C5 !important;
}

But the problem can also be in that a bootstrap style has higher precendence than you're css class. 但问题还在于引导样式比你的css类具有更高的优先级。 Make it more specific. 使它更具体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM