【问题标题】:Prevent child element css from over writing防止子元素 css 过度写入
【发布时间】:2015-01-15 15:32:45
【问题描述】:

我有两个 div。内部 div(绝对定位)放置在外部 Div(相对定位)之下。 我在外部 div 上应用了 jquery 切换事件来显示和隐藏内部 div。我还想在单击完成时更改外部 div 的不透明度,然后在第二次单击时外部 div 不透明度应再次设置为正常。

但问题在于外部 div ,内部 div 的不透明度也在设置,即使我在 css 中应用了重要标志也使子类更加具体。

这里是子类的css:

> div#child.childclass {
      position:absolute;
      display:none;
      width:100px;
      left:15px;
      top:-20px;
      background-color:red;
      font-size:12px;
      text-align:center;
      opacity:1 !important;
  }

这里的小提琴示例:fiddle for above issue

谢谢

【问题讨论】:

  • 由于您将不透明度应用于父元素,因此您无法将子元素中的不透明度设置回默认值,它将继承父元素的不透明度。
  • 使用 rgba 背景颜色代替不透明度?

标签: jquery html css css-selectors


【解决方案1】:

元素的不透明度值会影响其内容,进而影响其所有子元素。

该值适用于整个元素,包括其内容, 即使该值不被子元素继承。因此,一个 元素及其包含的子元素都具有相同的不透明度相对 到元素的背景,即使元素及其子元素有 相对于彼此不同的不透明度。

来源:MDN

解决方法:

在您的情况下,您可以使用rgba() 颜色作为绿点和边框。 rgba() 是具有不透明度的颜色值。这将改变颜色的不透明度:绿点和边框颜色而不影响子元素:

DEMO

;
var $p = $("div#parent");
var $c = $("div#child");

function firstFn(e) {
    if (e.target != e.currentTarget) { //not correct div
        console.log(e.target);
        return;
    }
    console.log("first click");
    $c.css("display", 'block');
    $p.addClass("active");

}

function secondFn(e) {
    if (e.target != e.currentTarget) { //not correct div
        console.log(e.target);
        return;
    }
    console.log("second click");
    $c.css("display", 'none');
    $p.removeClass("active");


}


// adding toggle function
$p.toggle(

function (e) {
    firstFn(e);
},

function (e) {
    secondFn(e);
});
  div#parent {
      width:20px;
      background-color:green;
      height:20px;
      margin:50px;
      cursor:pointer;
      position:relative;
      border:1px solid black;
      border-radius:100%;
  }
  div#child.childclass {
      position:absolute;
      display:none;
      width:100px;
      left:15px;
      top:-20px;
      background-color:red;
      font-size:12px;
      text-align:center;
      opacity:1 !important;
  }
  div#parent.active {
      background-color:rgba(0, 128, 0, .5);
      border:1px solid rgba(0, 0, 0, .5);
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
toggle clicks on green circle
<div id="container">
    <div id="parent">
        <div id="child" class="childclass">some text here</div>
    </div>
</div>

【讨论】:

  • 谢谢,它解决了这个问题。但我不明白为什么不透明度会覆盖孩子但 rgba() 不是?尽管我让孩子更具体并使用 !important
  • @Bhupendra 检查更新的答案,希望你能更好地理解。
【解决方案2】:

将父子元素分开并按照#container定位

演示 - http://jsfiddle.net/victor_007/Lk8h0xxg/7/

var $p = $("div#parent");
var $c = $("div#child");

function firstFn(e) {
  if (e.target != e.currentTarget) { //not correct div
    console.log(e.target);
    return;
  }
  console.log("first click");
  $c.css("display", 'block');
  $p.addClass("active");

}

function secondFn(e) {
  if (e.target != e.currentTarget) { //not correct div
    console.log(e.target);
    return;
  }
  console.log("second click");
  $c.css("display", 'none');
  $p.removeClass("active");


}


// adding toggle function
$p.toggle(

  function(e) {
    firstFn(e);
  },

  function(e) {
    secondFn(e);
  });
div#container {
  position: relative;
}
div#parent {
  width: 20px;
  background-color: green;
  height: 20px;
  margin: 50px;
  cursor: pointer;
  position: relative;
  border: 1px solid black;
  border-radius: 100%;
}
div#child.childclass {
  position: absolute;
  display: none;
  width: 100px;
  left: 46px;
  top: -20px;
  background-color: red;
  font-size: 12px;
  text-align: center;
  opacity: 1 !important;
}
.active {
  opacity: .5
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
toggle clicks on green circle
<div id="container">
  <div id="parent"></div>
  <div id="child" class="childclass">some text here</div>
</div>

【讨论】:

  • 感谢 Vitorino 的回答,我无法将子元素与父元素分开,因为子元素不会收到通知,我需要将内部 chid(绝对)放置到父元素(相对)
  • 您可以使用#container 来提供position:relative,就像在我的示例中一样
【解决方案3】:

#container { position: relative; }  

div#parent {
      width:20px;
      background-color:green;
      height:20px;
      margin:50px;
      cursor:pointer;
      position:relative;
      border:1px solid black;
      border-radius:100%;
  }
  div#child.childclass {
      position:absolute;
      width:100px;
      left:15px;
      top:-20px;
      background-color:red;
      font-size:12px;
      text-align:center;
      opacity:1 !important;
  }
  .active {
      opacity:.5
  }
<div id="container">
    <div id="child" class="childclass">some text here</div>
    <div id="parent" class="active"></div>
</div>

简单地说,子元素不能比它的父元素有更大的不透明度。

一旦您设置了一个元素的不透明度,那么它以及它的所有子元素(以及它的所有子元素的子元素等)都将继承该不透明度。

即使你这样做:

#parent { opacity:0.5; }
#child { opacity: 0.7; }

#child 实际上的不透明度为 0.35(这是不透明度 0.5 的 70% - 它的父项的不透明度)。

唯一的解决方案是在您的示例中使用#child,而不是#parentdescendent,如下所示:

http://jsfiddle.net/Lk8h0xxg/6/

【讨论】:

  • 嗨亚当,感谢您的回答,但我只想将子元素相对于父元素放置。为什么 !Important 不能防止内部 div 被覆盖..
  • 然后您需要添加另一个容器元素 - 修改您的 HTML。底线是,孩子的不透明度不能高于其父母。如果您在定位 HTML 元素时遇到问题,请随时提出其他问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-18
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-14
  • 1970-01-01
相关资源
最近更新 更多