คุณสมบัติ HTML DOM Textarea readOnly ส่งคืนและแก้ไขว่าเนื้อหาขององค์ประกอบพื้นที่ข้อความในเอกสาร HTML ควรเป็นแบบอ่านอย่างเดียวหรือไม่
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
1. กลับมาอ่านอย่างเดียว
object.readOnly
2. กำลังเพิ่มอ่านอย่างเดียว
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
