如何使一个检查按钮? (隐藏复选框,标签为按钮:仅CSS) [英] How to make a Check Button? (hidden checkbox with label as a button: CSS only)
本文介绍了如何使一个检查按钮? (隐藏复选框,标签为按钮:仅CSS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用方法1创建可点击标签,用CSS隐藏复选框和切换它和带jQuery的标签,我得到了this:
< pre(){$(this).toggleClass('active'); var(''')'; cbx = $('input',this); cbx.prop('checked',!cbx.prop(checked)); console.log($('input',this) ; 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 =checkboxchecked />检查按钮< / label>
[复选框状态]
我的问题是:是否可以只使用CSS 做同样的操作?
我意识到我使用< label>
不是< button>
是更好的方法来实现这个与jQuery。如果你考虑回复一个,请使用复选框(或至少解释为什么你不能使用它)。
解决方案
尝试css伪代码:
input.chk-btn {display:none;} input.chk-btn + label {border:1px solid灰色;背景:ghoswhite; padding:5px 8px; cursor:pointer; border-radius:5px;} input.chk-btn + label:active,input.chk-btn:checked + label {box-shadow:0px 0px 3px inset; background:#eee;}
< input type = checkboxid ='c1'class ='chk-btn'/>< label for ='c1'>检查按钮< / label>< input type =checkboxid ='c2'class = -btn'/>< label for ='c2'>切换< / label>
b
Using method 1 to create a clickable label, hiding the checkbox with CSS and toggling it and the label with jQuery, I got this:
$('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>
[Check the console for the checkbox status]
My question is: is it possible to do the same only using CSS?
I realize I'm using <label>
not <button>
and I assume there are better ways to achieve this with jQuery. If you consider replying with one, please do it using a checkbox (or at least explain why you must not use it).
解决方案
Try css pseudo code:
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>
这篇关于如何使一个检查按钮? (隐藏复选框,标签为按钮:仅CSS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文