HTML5 autofocus属性

本文介绍如何使用HTML中的autofocus属性及JavaScript的focus()函数来控制网页元素的焦点。当设置autofocus属性为true时,页面加载完成后该元素会自动获得焦点;通过JavaScript的focus()函数也能达到同样的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!doctype html>
<html>
<head>
<style>
#test1{
width:221px;
}
</style>
<script>
function setfocus(){
var test2=document.getElementById("test2");
test2.focus();//focus()函数,使元素获得焦点。blur()函数使元素失去焦点;
}
</script>
<meta charset="UTF-8">
</head>
<body>
<fieldset id="test1">
<legend>
autofocus属性:
</legend>
<p>账户:<input type="text" id="test2" autofocus="true"></p><!--设置autofocus属性为true后,可以在页面加载完成时使元素获得焦点-->
<p>密码:<input type="text" id="test3" autofocus="true"></p><!--如果所有元素都设置了autofocus属性,则页面加载完成时,第一个设置了autofocus属性的元素获得焦点-->
&nbsp&nbsp&nbsp<input type="button" value="提交" onclick="setfocus()">&nbsp&nbsp<input type="button" value="取消">
</fieldset>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值