简体   繁体   English

未选中时更改单选按钮颜色

[英]change radio button color when not selected

I have radio buttons + pictures. 我有单选按钮+图片。 But problem is if page are been loaded all radio buttons got a color:none. 但问题是如果页面已加载所有单选按钮都有颜色:无。 (that is good) if i click on first button than color become grey and button :selected but if i click on secound button then button change color and :selected but first button color still are grey. (这很好)如果我点击第一个按钮比颜色变成灰色和按钮:选中但如果我点击secound按钮然后按钮改变颜色和:选择但第一个按钮颜色仍然是灰色。 How to deselect color on first button. 如何在第一个按钮上取消选择颜色。

<div id="my_div">
input type="radio" name="site" id="so"><label for="so"><img src="/icons/disp.jpg"><br>Title1</label>
<input type="radio" name="site" id="sf"><label for="sf"><img src="/icons/metal.jpg"><br>MetTitle2</label>
</div>

jquery jQuery的

$('#my_div input:radio').addClass('input_hidden');
$('#my_div label').click(function() {
    $(this).addClass('selected').siblings().removeClass('selected');
});

CSS CSS

.input_hidden {
    position: absolute;
    display:none;

}

.selected {
    background-color: #ccc;
}



#my_div label {
    display: inline-block;
    cursor: pointer;
}


#my_div label:hover {
    background-color:forestgreen;
}

#my_div label img {
    padding: 3px;

}

You could achieve this by using pure CSS like this: 您可以通过使用像这样的纯CSS来实现这一点:

 input[type="radio"]:checked+label { background-color: #ccc; } .input_hidden { position: absolute; display:none; } #my_div label { display: inline-block; cursor: pointer; } #my_div label:hover { background-color:forestgreen; } #my_div label img { padding: 3px; } 
 <div id="my_div"> <input type="radio" name="site" id="so"><label for="so"><img src="/icons/disp.jpg"><br>Title1</label> <input type="radio" name="site" id="sf"><label for="sf"><img src="/icons/metal.jpg"><br>MetTitle2</label> </div> 

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

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