当选择单选按钮时,有没有办法为单选按钮的颜色变化设置标签?
我希望标签与hover
状态下的绿色相同,但我似乎无法弄清楚如何实现这一目标?
label.btn span {
font-size: 16px;
}
i.fa {
font-size: 24px;
}
i.fa.fa-dot-circle-o {
font-size: 24px;
}
label input[type="radio"] {
color: #c8c8c8;
display: inline;
}
label input[type="radio"] ~ i.fa.fa-dot-circle-o {
display: none;
}
label input[type="radio"]:checked ~ i.fa.fa-circle-o {
display: none;
}
label input[type="radio"]:checked ~ i.fa.fa-dot-circle-o {
color: #78a025;
display: inline;
font-size: 24px;
}
label:hover input[type="radio"] ~ i.fa {
color: #78A025;
}
label input[type="checkbox"] ~ i.fa.fa-square-o {
color: #c8c8c8;
display: inline;
}
label input[type="checkbox"] ~ i.fa.fa-check-square-o {
display: none;
}
label input[type="checkbox"]:checked ~ i.fa.fa-square-o {
display: none;
}
label input[type="checkbox"]:checked ~ i.fa.fa-check-square-o {
color: #78A025;
display: inline;
}
label:hover input[type="checkbox"] ~ i.fa {
color: #78a025;
}
div[data-toggle="buttons"] label.active {
color: #78a025;
}
div[data-toggle="buttons"] label {
display: inline-block;
padding: 6px 12px;
margin-bottom: 0;
font-size: 14px;
font-weight: normal;
line-height: 2em;
text-align: left;
white-space: nowrap;
vertical-align: top;
cursor: pointer;
background-color: none;
border: 0px solid #c8c8c8;
border-radius: 3px;
color: #c8c8c8;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
}
div[data-toggle="buttons"] label:hover {
color: #78A025;
}
div[data-toggle="buttons"] label:active,
div[data-toggle="buttons"] label.active {
-webkit-box-shadow: none;
box-shadow: none;
}
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet">
<div class="row">
<div class="col-xs-12">
<br>Vertical radio:
<br>
<div class="btn-group btn-group-vertical" data-toggle="buttons">
<label class="btn">
<input type="radio" name='gender1' checked><i class="fa fa-circle-o fa-2x"></i><i class="fa fa-dot-circle-o fa-2x"></i> <span> Male</span>
</label>
<label class="btn">
<input type="radio" name='gender1'><i class="fa fa-circle-o fa-2x"></i><i class="fa fa-dot-circle-o fa-2x"></i><span> Female</span>
</label>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<hr>Vertical checkbox:
<br>
<div class="btn-group btn-group-vertical" data-toggle="buttons">
<label class="btn">
<input type="checkbox" name='email1' checked><i class="fa fa-square-o fa-2x"></i><i class="fa fa-check-square-o fa-2x"></i> <span> Marketing Email</span>
</label>
<label class="btn">
<input type="checkbox" name='email2'><i class="fa fa-square-o fa-2x"></i><i class="fa fa-check-square-o fa-2x"></i><span> Alert Email</span>
</label>
<label class="btn">
<input type="checkbox" name='email3'><i class="fa fa-square-o fa-2x"></i><i class="fa fa-check-square-o fa-2x"></i><span> Account Email</span>
</label>
</div>
</div>
</div>
这是一个用于演示目的的编译工具cdpn.io/ENMMOz
答案 0 :(得分:7)
您无法设置label
的样式(因为我们在CSS中没有父选择器),但您可以为{{1}设置文本的样式}或radiobutton
- 预期结果 - 使用此:
checkbox
见下面的演示:
label input[type="radio"]:checked ~ span {
color: #78a025;
}
label input[type="checkbox"]:checked ~ span {
color: #78a025;
}
label.btn span {
font-size: 16px;
}
i.fa {
font-size: 24px;
}
i.fa.fa-dot-circle-o {
font-size: 24px;
}
label input[type="radio"] {
color: #c8c8c8;
display: inline;
}
label input[type="radio"] ~ i.fa.fa-dot-circle-o {
display: none;
}
label input[type="radio"]:checked ~ i.fa.fa-circle-o {
display: none;
}
label input[type="radio"]:checked ~ i.fa.fa-dot-circle-o {
color: #78a025;
display: inline;
font-size: 24px;
}
label:hover input[type="radio"] ~ i.fa {
color: #78A025;
}
label input[type="checkbox"] ~ i.fa.fa-square-o {
color: #c8c8c8;
display: inline;
}
label input[type="checkbox"] ~ i.fa.fa-check-square-o {
display: none;
}
label input[type="checkbox"]:checked ~ i.fa.fa-square-o {
display: none;
}
label input[type="checkbox"]:checked ~ i.fa.fa-check-square-o {
color: #78A025;
display: inline;
}
label:hover input[type="checkbox"] ~ i.fa {
color: #78a025;
}
div[data-toggle="buttons"] label.active {
color: #78a025;
}
div[data-toggle="buttons"] label {
display: inline-block;
padding: 6px 12px;
margin-bottom: 0;
font-size: 14px;
font-weight: normal;
line-height: 2em;
text-align: left;
white-space: nowrap;
vertical-align: top;
cursor: pointer;
background-color: none;
border: 0px solid #c8c8c8;
border-radius: 3px;
color: #c8c8c8;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
}
div[data-toggle="buttons"] label:hover {
color: #78A025;
}
div[data-toggle="buttons"] label:active,
div[data-toggle="buttons"] label.active {
-webkit-box-shadow: none;
box-shadow: none;
}
label input[type="radio"]:checked ~ span {
color: #78a025;
}
label input[type="checkbox"]:checked ~ span {
color: #78a025;
}
答案 1 :(得分:3)
选中span
后,尝试使用label
更改颜色,使用 ~
运算符,如:
label input[type="radio"]:checked ~ span {
color: #78a025;
}
label input[type="checkbox"]:checked ~ span {
color: #78a025;
}
请看下面的代码段:
label.btn span {
font-size: 16px ;
}
i.fa{
font-size: 24px;
}
i.fa.fa-dot-circle-o{
font-size: 24px;
}
label input[type="radio"]{
color: #c8c8c8; display: inline;
}
label input[type="radio"] ~ i.fa.fa-dot-circle-o{
display: none;
}
label input[type="radio"]:checked ~ i.fa.fa-circle-o{
display: none;
}
label input[type="radio"]:checked ~ i.fa.fa-dot-circle-o{
color: #78a025;
display: inline;
font-size: 24px;
}
label input[type="radio"]:checked ~ span {
color: #78a025;
}
label:hover input[type="radio"] ~ i.fa {
color: #78A025;
}
label input[type="checkbox"] ~ i.fa.fa-square-o{
color: #c8c8c8; display: inline;
}
label input[type="checkbox"] ~ i.fa.fa-check-square-o{
display: none;
}
label input[type="checkbox"]:checked ~ i.fa.fa-square-o{
display: none;
}
label input[type="checkbox"]:checked ~ i.fa.fa-check-square-o{
color: #78A025; display: inline;
}
label:hover input[type="checkbox"] ~ i.fa {
color: #78a025;
}
div[data-toggle="buttons"] label.active{
color: #78a025;
}
label input[type="checkbox"]:checked ~ span {
color: #78a025;
}
div[data-toggle="buttons"] label {
display: inline-block;
padding: 6px 12px;
margin-bottom: 0;
font-size: 14px;
font-weight: normal;
line-height: 2em;
text-align: left;
white-space: nowrap;
vertical-align: top;
cursor: pointer;
background-color: none;
border: 0px solid
#c8c8c8;
border-radius: 3px;
color: #c8c8c8;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
}
div[data-toggle="buttons"] label:hover {
color: #78A025;
}
div[data-toggle="buttons"] label:active, div[data-toggle="buttons"] label.active {
-webkit-box-shadow: none;
box-shadow: none;
}
&#13;
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet">
<div class="row">
<div class="col-xs-12">
<br> Vertical radio:
<br>
<div class="btn-group btn-group-vertical" data-toggle="buttons">
<label class="btn">
<input type="radio" name='gender1' checked><i class="fa fa-circle-o fa-2x"></i><i class="fa fa-dot-circle-o fa-2x"></i> <span> Male</span>
</label>
<label class="btn">
<input type="radio" name='gender1'><i class="fa fa-circle-o fa-2x"></i><i class="fa fa-dot-circle-o fa-2x"></i><span> Female</span>
</label>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<hr> Vertical checkbox:
<br>
<div class="btn-group btn-group-vertical" data-toggle="buttons">
<label class="btn">
<input type="checkbox" name='email1' checked><i class="fa fa-square-o fa-2x"></i><i class="fa fa-check-square-o fa-2x"></i> <span> Marketing Email</span>
</label>
<label class="btn">
<input type="checkbox" name='email2'><i class="fa fa-square-o fa-2x"></i><i class="fa fa-check-square-o fa-2x"></i><span> Alert Email</span>
</label>
<label class="btn">
<input type="checkbox" name='email3'><i class="fa fa-square-o fa-2x"></i><i class="fa fa-check-square-o fa-2x"></i><span> Account Email</span>
</label>
</div>
</div>
</div>
&#13;
希望这有帮助!
答案 2 :(得分:3)
编辑:哇!我完全错过了你的文本跨度实际上在标签本身内的事实,所以你的解决方案很可能在CSS中实现。而不是在这里提供你的答案,似乎Kukkuz's answer已经完全正确!
我将在这里留下这个答案,这样你就可以看到jQuery解决方案的语法可能是什么样的,尽管我当然建议尽可能使用纯CSS!
正如你在评论中提到的,你对jQuery解决方案持开放态度,所以下面我提供了一个。
以下是一个示例:https://jsfiddle.net/j7n9zyaj/
代码:
//When a checkbox/radio is clicked...
$('input[type=radio], input[type=checkbox]').on("click", function() {
var $t = $(this);
//If it's checked
if ($t.is(':checked')) {
//Add the green class to the parent
$t.parent().addClass("greenText");
} else {
//Remove the green class from the parent
$t.parent().removeClass("greenText");
}
});
你只需要将这个简单的类添加到CSS中:
.greenText {
color: #78A025 !important;
}