label的作用
表示用户界面中某个元素的说明
增加命中区域,屏幕阅读器可以读出标签。使使用辅助技术的用户更容易理解输入 哪些数据
用法
单击关联标签激活input,需给input一个id属性,给label一个for属性,设为同一个值
规定 label 字段所属的一个或多个表单。
http://www.w3school.com.cn/tiy/t.asp?f=html5_label_form
注意事项
一个 input 可以与多个标签相关联。
标签本身并不与表单直接相关。它们只通过与它们相关联的控件间接地与表单相关联。
当点击或者触碰(tap)一个与表单控件相关联的 时,关联的控件的 click 事件也会触发。
举例
input与label互相关联的机制,具体实例:
- 利用label"模拟"button来解决不同浏览器原生button样式不同的问题
<input type="button" id="btn">
<label for="btn">Button</label>
<style>
input[type='button'] {
display: none;
}
label {
display: inline-block;
padding: 10px 20px;
background: #456;
color: #fff;
cursor: pointer;
box-shadow: 2px 2px 4px 0 rgba(0,0,0,.3);
border-radius: 2px;
}
</style>
- 结合checkbox、radio表单元素实现纯CSS状态切换,这样的实例就太多了。比如控制CSS动画播放和停止。下面是一部分代码。详细实例地址
<input type="checkbox" id="controller">
<label class="icon" for="controller">
<div class="play"></div>
<div class="pause"></div>
</label>
<div class="animation"></div>
<style>
...
#controller:checked ~ .animation {
animation-play-state: paused;
}
...
</style>
- 还有一个基于 radio 的实例:摩斯密码键盘
input的focus事件会触发锚点定位,我们可以利用label当触发器实现选项卡切换效果。下面代码选自张鑫旭《CSS世界》。实际效果链接
<div class="box">
<div class="list"><input id="one" readonly>1</div>
<div class="list"><input id="two" readonly>2</div>
<div class="list"><input id="three" readonly>3</div>
<div class="list"><input id="four" readonly>4</div>
</div>
<div class="link">
<label class="click" for="one">1</label>
<label class="click" for="two">2</label>
<label class="click" for="three">3</label>
<label class="click" for="four">4</label>
</div>
<style>
.box {
width: 20em;
height: 10em;
border: 1px solid #ddd;
overflow: hidden;
}
.list {
height: 100%;
background: #ddd;
text-align: center;
position: relative;
}
.list > input {
position: absolute; top:0;
height: 100%; width: 1px;
border:0; padding: 0; margin: 0;
clip: rect(0 0 0 0);
}
</style>