选中子输入时更改父div的背景颜色

时间:2015-10-19 13:34:53

标签: html css

这是代码。我需要的是在选中“Female”输入时更改.register-switch的背景颜色。 我试过了:

input[value="Female"]:checked + .register-switch { 
  background: red;
}

但它不起作用。不使用JavaScript就可以达到预期的效果吗?

.register-switch {
  height: 32px;
  margin-bottom: 20px;
  padding: 4px;
  background: lightblue;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}
.register-switch-input {
  display: none;
}
.register-switch-label {
  float: left;
  width: 50%;
  line-height: 32px;
  color: #fff;
  text-align: center;
  cursor: pointer;
}
.register-switch-input:checked + .register-switch-label {
  font-weight: normal;
  color: #666;
  background: #fff;
  border-radius: 2px;
  background-image: -webkit-linear-gradient(top, #fefefe, #eeeeee);
  background-image: -moz-linear-gradient(top, #fefefe, #eeeeee);
  background-image: -o-linear-gradient(top, #fefefe, #eeeeee);
  background-image: linear-gradient(to bottom, #fefefe, #eeeeee);
  -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.1);
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.1);
}
<div class="register-switch">
  <input type="radio" name="sex" value="Male" id="sex-m" class="register-switch-input" checked>
  <label for="sex-m" class="register-switch-label">Male</label>
  <input type="radio" name="sex" value="Female" id="sex-f" class="register-switch-input">
  <label for="sex-f" class="register-switch-label">Female</label>
</div>

2 个答案:

答案 0 :(得分:6)

你可以尝试这样的事情。

不是将背景颜色应用于父元素,而是创建一个100%高度和宽度的:after pseudo-element并将背景应用于after以后可以使用+ selector.make确保在父元素position:relative上设置.register-switch

&#13;
&#13;
.register-switch {
  height: 32px;
  margin-bottom: 20px;
  padding: 4px;
  position:relative;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}
.register-switch-input {
  display: none;
}
.register-switch-label {
  float: left;
  width: 50%;
  line-height: 32px;
  color: #fff;
  text-align: center;
  cursor: pointer;
}

.register-switch-label:after{
  position:absolute;
  content:"";
  width:100%;
  height:100%;
  background:lightblue;
  left:0;
  top:0;
  z-index:-1;
  }
.register-switch-input:checked + .register-switch-label:after{
  background:tomato;
  }
.register-switch-input:checked + .register-switch-label {
  font-weight: normal;
  color: #666;
  background: #fff;
  border-radius: 2px;
  background-image: -webkit-linear-gradient(top, #fefefe, #eeeeee);
  background-image: -moz-linear-gradient(top, #fefefe, #eeeeee);
  background-image: -o-linear-gradient(top, #fefefe, #eeeeee);
  background-image: linear-gradient(to bottom, #fefefe, #eeeeee);
  -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.1);
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.1);
}
&#13;
<div class="register-switch">
  <input type="radio" name="sex" value="Male" id="sex-m" class="register-switch-input" checked>
  <label for="sex-m" class="register-switch-label">Male</label>
  <input type="radio" name="sex" value="Female" id="sex-f" class="register-switch-input">
  <label for="sex-f" class="register-switch-label">Female</label>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以使用jQuery选择无线电输入的父元素,并在更改输入时添加/删除类。

<div class="register-switch">
    <input type="radio" name="sex" value="Male" id="sex-m" class="register-switch-input" checked>
    <label for="sex-m" class="register-switch-label">Male</label>
    <input type="radio" name="sex" value="Female" id="sex-f" class="register-switch-input">
    <label for="sex-f" class="register-switch-label">Female</label>
</div>
.clicked {
    background-color: red;
}
$( '.register-switch-input' ).on( 'change', function() {
    $this = $( this );
    $this.parent().addClass( 'clicked' );
} );

http://jsfiddle.net/yfh301od/