HTML DOM 输入复选框 autofocus 属性
htmlweb developmentfront end technology更新于 2025/6/22 22:36:17
HTML DOM 输入复选框 autofocus 属性返回并修改 type="checkbox"的 HTML 输入元素的 autofocus 属性值。
语法
以下是语法 −
1. 返回 autofocus
object.autofocus
2. 修改 autofocus
object.autofocus = true|false
示例
让我们看一个 autofocus 属性的示例 −
<!DOCTYPE html>
<html>
<head>
<title<HTML DOM autofocus property</title<
<style>
body{
text-align:center;
}
p{
font-size:1.5rem;
color:#ff8741;
}
input{
width:30px;
height:30px;
outline:2px solid #db133a;
}
button{
background-color:#db133a;
color:#fff;
padding:8px;
border:none;
width:120px;
margin:0.5rem;
border-radius:50px;
outline:none;
}
</style>
</head>
<body>
<h1>autofocus Property Example</h1>
<p>Do you need help?</p>
<input type="checkbox" autofocus>
<br>
<button onclick="yes()">Yes</button>
<button onclick="no()">No</button>
<script>
function yes() {
document.querySelector("input").checked = true;
}
function no() {
document.querySelector("input").checked = false;
}
</script>
</body>
</html>
输出
这将产生以下输出 −

单击"是/否"按钮来选中或取消选中复选框,但正如您所见,当页面加载时,复选框已经以红色轮廓自动对焦。


相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

