HTML DOM Textarea maxLength 属性
htmldomweb developmentfront end technology更新于 2024/5/21 23:40:17
HTML DOM Textarea 的 maxLength 属性用于返回并修改 HTML 文档中文本区域元素的 maxLength 属性值。
语法
以下是语法 −
1. 返回 maxLength
object.maxLength
2. 修改 maxLength
object.maxLength = “number”
让我们看一个 HTML DOM Textarea 的 maxLength 属性示例:
示例
<!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;
}
.show {
font-size: 1.5rem;
margin: 1rem 0;
}
</style>
<body>
<h1>DOM Textarea maxLength Property Demo</h1>
<textarea maxlength="50">Hi! I'm a text area element with some dummy text.</textarea>
<button onclick="set()" class="btn">Show Maximum Length</button>
<div class="show"></div>
<script>
function set() {
document.querySelector('.show').innerHTML = 'The maximum number of characters allowed in the above text area is ' + document.querySelector("textarea").maxLength;
}
</script>
</body>
</html>
输出

点击"显示最大长度"按钮显示textarea元素的maxLength属性值。

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

