使用 method 1 to create a clickable label , hiding the checkbox with CSS和 toggling it and the label with jQuery ,我得到了这个:
$('label').click(function(e) {
$(this).toggleClass('active');
var cbx = $('input', this);
cbx.prop('checked', !cbx.prop("checked"));
console.log($('input', this).prop('checked'));
return false;
})
input[type="checkbox"] {visibility: hidden;position: absolute}
label {background-color: pink;border: 1px solid lightblue;padding: 20px}
label.active {background-color: lightgreen}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<input type="checkbox" checked />Check Button</label>
我的问题是:是否有可能只使用 CSS 做同样的事情?
我意识到我正在使用 <label>
不是<button>
我认为有更好的方法可以使用 jQuery 实现这一点。如果您考虑用一个回复,请使用复选框(或至少解释为什么不能使用它)。
最佳答案
试试css伪代码:
input.chk-btn {
display: none;
}
input.chk-btn + label {
border: 1px solid grey;
background: ghoswhite;
padding: 5px 8px;
cursor: pointer;
border-radius: 5px;
}
input.chk-btn:not(:checked) + label:hover {
box-shadow: 0px 1px 3px;
}
input.chk-btn + label:active,
input.chk-btn:checked + label {
box-shadow: 0px 0px 3px inset;
background: #eee;
}
<input type="checkbox" id='c1' class='chk-btn' />
<label for='c1'>Check Button</label>
<input type="checkbox" id='c2' class='chk-btn' />
<label for='c2'>Toggle</label>
关于jquery - 如何制作复选按钮? (带有标签作为按钮的隐藏复选框 : CSS only),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30100978/