HTML DOM Textarea readOnly 属性
htmldomweb developmentfront end technology更新于 2024/3/5 6:04:17
HTML DOM Textarea 的 readOnly 属性用于返回并修改 HTML 文档中文本区域元素的内容是否只读。
语法
以下是语法 −
1. 返回 readOnly
object.readOnly
2. 添加 readOnly
object.readOnly = true | false
让我们看一个 HTML DOM Textarea readOnly 属性的示例 −
示例
<!DOCTYPE html>
<html>
<style>
body {
color: #000;
background: lightseagreen;
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 readOnly Property Demo</h1>
<textarea rows="5" cols='20' name="I'm name attribute of textarea element">
Hi! I'm a text area element with some dummy text.
</textarea>
<button onclick="set()" class="btn">Toggle Read Only</button>
<script>
function set() {
var ta = document.querySelector("textarea");
if (ta.readOnly === true) {
ta.readOnly = false;
} else {
ta.readOnly = true;
}
}
</script>
</body>
</html>
输出

点击"切换只读"按钮来切换textarea元素的readOnly属性的值。

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

