我得到了一个带方形单选按钮的设计.

有没有办法将单选按钮设置为这样?
唯一的方法,我认为我可以通过使用图像来实现这一点,并通过一种$.on('click')方法交换已检查/检查状态.
Jac*_*ray 18
只用CSS就可以轻松完成,不需要JS.基本概念是设置一个元素作为输入的兄弟,创建一个"假的"单选按钮:
/*
* Hide the inputs.
*/
input {
display: none;
}
/*
* Then, style the label so it looks like however you want.
* Here's a quick rundown of how I did it here:
*/
/*
* Some basic positioning styles, and we give it the pointer cursor to show
* that it's clickable
*/
label {
display: inline-block;
padding: 5px 10px;
cursor: pointer;
}
/*
* With how I decided to build this, the position: relative is super important.
* We're going to position a pseudo element within this element(As it is the containing box)
*/
label span {
position: relative;
line-height: 22px;
}
/*
* Because we're using pseudo elements, a content property is required to make them appear.
*/
label span:before,
label span:after {
content: '';
}
/*
* We are using the :before peudo elemnt as the actual button,
* then we'll position the :after over it. You could also use a background-image,
* font-icon, or really anything if you want different styles.
* For the specific style we're going for, this approach is simply the easiest, but
* once you understand the concept you can really do it however you like.
*/
label span:before {
border: 1px solid #222021;
width: 20px;
height: 20px;
margin-right: 10px;
display: inline-block;
vertical-align: top;
}
label span:after {
background: #222021;
width: 14px;
height: 14px;
position: absolute;
top: 2px;
left: 4px;
transition: 300ms;
opacity: 0;
}
/*
* This is the most important part of this whole file, if you understand what's happening here
* you can really make this in so many different ways.
*
* We start by selecting the input inside of the label, with "label input". From there we use the
* ":checked" selector to *only* select the input when it is checked. We then use the immediate sibling
* selector(+) to select the span, and then it's pseudo element :after(What we are using to mark the button)
* Because we already styled the :after, all we have to do is set the opacity to 1, making it fade in.
*/
label input:checked+span:after {
opacity: 1;
}
/*
* A little styling for the demo
*/
body {
background: #fbfbfb;
font-family: Arial;
font-weight: bold;
color: rgba(0, 0, 0, 0.7);
}Run Code Online (Sandbox Code Playgroud)
<label>
<input type="radio" name="radio">
<span>EMAIL</span>
</label>
<label>
<input type="radio" name="radio">
<span>PHONE</span>
</label>Run Code Online (Sandbox Code Playgroud)
查看代码注释以获得更深入的解释,但这是基础知识:
首先创建一个<label>包装器.我们使用标签,因为在它上面触发的事件也将在相关输入上触发:
<label></label>
Run Code Online (Sandbox Code Playgroud)
添加输入:
<label>
<input type="radio" name="demo">
</label>
Run Code Online (Sandbox Code Playgroud)
请记住,单选按钮必须具有相同的名称才能进行分组.现在我们<span>在输入之后抛出一个,所以我们在CSS中有一些目标.
<label>
<input type="radio" name="demo">
<span></span>
</label>
Run Code Online (Sandbox Code Playgroud)
并且HTML已全部设置完毕.检查CSS中的解释,它会更容易理解.
您不需要设置单选按钮的样式。只需使用一个div:
小提琴示例:http : //jsfiddle.net/kLGf4/2/
html:
<section>
<header>
<h1>Perfered Method of Contact</h1>
</header>
<div> <span>Choice 1</span>
<div class="square-radio square-radio--clicked">
<div class="square-radio--content"></div>
</div>
</div>
<div> <span>Choice 2</span>
<div class="square-radio">
<div class="square-radio--content"></div>
</div>
</div>
</section>
Run Code Online (Sandbox Code Playgroud)
css:
.square-radio {
border: 1px solid black;
margin: 2px;
width: 40px;
height: 40px;
position: relative;
}
.square-radio--clicked .square-radio--content{
margin: auto;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background-color: black;
width: 20px;
height: 20px;
}
Run Code Online (Sandbox Code Playgroud)
js:
$(document).ready(function () {
$(document).on("click", ".square-radio", function () {
$(this).toggleClass("square-radio--clicked");
});
});
Run Code Online (Sandbox Code Playgroud)
这是我所知道的最简单的,一个不需要标签或脚本的纯 CSS 解决方案。完全兼容需要几个供应商前缀:
input[type='radio'] {
box-sizing: border-box;
appearance: none;
background: white;
outline: 2px solid #333;
border: 3px solid white;
width: 16px;
height: 16px;
}
input[type='radio']:checked {
background: #333;
}
Run Code Online (Sandbox Code Playgroud)
如上所述,box-sizing和appearance属性应以供应商为前缀:
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
Run Code Online (Sandbox Code Playgroud)
小智 5
input[type=radio] {
padding: 0.5em;
-webkit-appearance: none;
outline: 0.1em solid black;
outline-offset: 0.1em;
}
input[type=radio]:checked {
display: inline-block;
background-color: #000;
}Run Code Online (Sandbox Code Playgroud)
<label for="radioA"><input type="radio" name="radio1" value="A"/> A</label><br>
<label for="radioB"><input type="radio" name="radio1" value="B"/> B</label><br>
<label for="radioC"><input type="radio" name="radio1" value="C"/> C</label><br>
<label for="radioD"><input type="radio" name="radio1" value="D"/> D</label>Run Code Online (Sandbox Code Playgroud)