(10)<label>标签的用途

一、<label>标签的作用

所有的表单元素都可以通过绑定其他内容去扩大触发点击范围,也就是<label>标签它的作用就是扩大了选区范围,可以不只是单击表单元素才能被选中,还可以点击表单元素对应的文字内容进行选中该表单元素,浏览器会自自动将焦点转到和标签相关的表单控件上,帮助表单元素定义标注(标记)。

二、绑定方法

1.绑定方法一

绑定步骤:

1.给表单元素设置id属性

<form>
        <input type="radio" name="sex" id="man"> 男
        <input type="radio" name="sex" id="women"> 女
</form>

2.然后将需要绑定的其他内容用<label>标签包裹

<form>
        <input type="radio" name="sex" id="man"><label> 男</label>
        <input type="radio" name="sex" id="women"><label> 女</label>
</form>

3.给<label>标签设置for属性,属性值为绑定的表单元素的id属性值。

<form>
        <input type="radio" name="sex" id="man"><label for="man"> 男</label>
        <input type="radio" name="sex" id="women"><label for="women"> 女</label>
</form>

适用场景:绑定方式一实用于绑定距离较远的表单元素。

2.绑定方法二

适用场景:绑定方式二实用于表单元素与内容紧挨着时进行绑定。

绑定步骤:

1.如果绑定内容和表单元素写在一起,可以化简绑定写法,直接使用<label>标签将绑定内容与表单元素一起进行嵌套。

<form>
        <label><input type="radio" name="sex">男</label>
        <label><input type="radio" name="sex">女</label>
</form>

通过如上示例,我们看到,绑定方法二更加简洁,省去了给表单元素设置id和<label>标签的for属性绑定的步骤。

请把一下代码每一行的用途注释上:<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>登录表单</title> <style> body { background-image: url("background.jpg"); background-size: cover; font-family: "宋体"; font-size: 30px; } form { margin: auto; width: 50%; background-color: rgba(255,255,255,0.8); padding: 20px; border-radius: 10px; } input[type=text], input[type=password] { width: 100%; padding: 12px 20px; margin: 8px 0; display: inline-block; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; } input[type=submit], input[type=reset] { background-color: #4CAF50; color: white; padding: 14px 20px; margin: 8px 0; border: none; border-radius: 4px; cursor: pointer; } input[type=submit]:hover, input[type=reset]:hover { background-color: #45a049; } .error { color: red; } </style> </head> <body> <form> <h2>登录</h2> <label for="username">用户名</label> <input type="text" id="username" name="username" ><br> <label for="password">密码</label> <input type="password" id="password" name="password" ><br> <label for="confirm_password">确认密码</label> <input type="password" id="confirm_password" name="confirm_password" ><br> <input type="submit" value="登录"> <input type="reset" value="重置"> <span class="error" id="password_error"></span><br> <a href="https://www.baidu.com/">忘记密码?</a> </form> <script> document.querySelector("form").addEventListener("submit", function(event) { var password = document.querySelector("#password").value; var confirm_password = document.querySelector("#confirm_password").value; if (password != confirm_password) { document.querySelector("#password_error").innerHTML = "密码不一致"; event.preventDefault(); } }); </script> </body> </html>
05-24
解释每条代码<style> fieldset{ width: 350px ; margin: 100px auto ; } p{ font-size: 0.8em; color: #999; text-indent: 2em; } li{ line-height: 40px; } label{ display:inline-block ; width: 200px; text-align: right; margin-right: 10px; } #height{ width: 60px; } #weight{ width: 60px; } </style> </head> <body> <fieldset> <legend><h2>标准体测测试</h2></legend> <p> 身体质量指数,是BMI(Body Mass Index )指数,简称体质指数,是国际上常用的衡量人 体胖瘦程度以及是否健康的一个标准。计算公式为:BMI=体重÷身高²。(体重单位:千克; 身高单位:米。)BMI由19世纪中期的比利时通才凯特勒最先提出。 </p> <ol> <li> <label for="height">请输入你的身高(cm):</label> <input type="number" id="height" min="40"> </li> <li> <label for="weight">请输入你的体重(kg):</label> <input type="number" id="weight" min="20"> </li> </ol> <input type="button" name="" id="myBtn" value="开始测试"> </fieldset> <script> var oBtn = document.getElementById("myBtn") var BMI oBtn.onclick=function(){ let result let oHeight = document.getElementById("height").value/100 let oweight = document.getElementById("weight").value BMI = oweight/(oHeight*oHeight) if(isNaN(BMI)) { alert("请输入合法数字") } else { if(BMI<18.5) result = "体重过轻" if(BMI>18.5 && BMI<=23.9) result = "体重正常" if(BMI>=24) result="体重超重" if(BMI>24 && BMI<=26.9) result = "肥胖前期" if(BMI>27 && BMI<=29.9) result = "I度肥胖" if(BMI>=30) result = "II度肥胖" if(BMI>=40) result = "III度肥胖" } alert(result) } </script>
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泽哥ins

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值