我使用下面的代码来创建一个切换开关,而不是单选按钮-它工作得很好
.switch
{
display: block;
float: left;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
font-weight: bold;
text-transform: uppercase;
background: #eee;
border: 1px solid #aaa;
padding: 2px;
box-shadow: inset 0 2px 5px rgba(0,0,0,0.2);
margin-left: 20px;
}
.switch input[type=radio]
{
display: none;
}
.switch label
{
display: block;
float: left;
padding: 3px 6px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
color: #aaa;
cursor: pointer;
}
.switch label:hover
{
text-shadow: 0 0 2px #fff;
color: #888;
}
.switch label.checked
{
background: #8fc800;
color: #eee;
text-shadow: 0 -1px 1px rgba(0,0,0,0.5);
background: -webkit-linear-gradient(top, #8fc800, #438c00);
background: -moz-linear-gradient(top, #8fc800, #438c00);
background: -ms-linear-gradient(top, #8fc800, #438c00);
cursor: default;
}和html:
<div class="switch">
<input type="radio" name="weekly_new" id="weekSW-0" <?=$yes_checked?> value="Yes" />
<label for="weekSW-0" id="yes">ON</label>
<input type="radio" name="weekly_new" id="weekSW-1" <?=$no_checked?> value="No" />
<label for="weekSW-1" id="no">OFF</label>
</div>和jquery:
<script>
$(".switch label:not(.checked)").click(function(){
var label = $(this);
var input = $('#' + label.attr('for'));
if (!input.prop('checked')){
label.closest('.switch').find("label").removeClass('checked');
label.addClass('checked');
input.prop('checked', true);
}
});
$(".switch input[checked=checked]").each(function(){
$("label[for=" + $(this).attr('id') + "]").addClass('checked');
});
</script>正如你所看到的,label.checked给了按钮一个“外观”--我想要做的是,当选择“否”而不是“是”时,有一种不同的颜色--红色代表“否”,绿色代表“是”,具有开创性的哈?
不管怎样,我都不知道这件事能不能做到
我给标签分配了I,并尝试了。
.switch label.checked #yes { blah blah }但是这使得样式变得无用!
因为我已经这样做了代码,这能做到吗?
我已经创建了一个小提琴,如果更容易摆弄的话...obv不得不像我通常从mysql表中加载那样放入valuse checked=checked,但它不能
http://jsfiddle.net/aS69U/
发布于 2012-10-30 17:26:12
这个CSS应该和你的代码一起工作:
.switch #no.checked {
background:red;
}顺便说一句,定制很酷;-)
https://stackoverflow.com/questions/13135942
复制相似问题