HTML DOM Textarea autofocus 属性
htmldomweb developmentfront end technology更新于 2025/11/29 20:28:17
HTML DOM Textarea 的 autofocus 属性用于返回并修改文本区域在页面加载时是否自动获得焦点。
语法
以下是语法 −
1. 返回 autofocus
object.autofocus
2. 修改 autofocus
object.autofocus = true | false
让我们看一个 HTML DOM Textarea autofocus 属性的示例:
示例
<!DOCTYPE html>
<html>
<style>
body {
color: #000;
height: 100vh;
}
.btn {
background: #db133a;
border: none;
height: 2rem;
border-radius: 2px;
width: 40%;
display: block;
color: #fff;
outline: none;
cursor: pointer;
}
</style>
<body>
<h1>DOM Textarea autofocus Property Demo</h1>
<textarea autofocus>Hi! I'm a text area element with some dummy text.</textarea>
<button onclick="set()" class="btn">Disable Autofocus</button>
<script>
function set() {
document.querySelector("textarea").autofocus = false;
}
</script>
</body>
</html>
输出

点击"禁用自动对焦"按钮,禁用文本区域元素的自动对焦功能。现在,光标将不可见:

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

