คุณสมบัติ textTransform ของ DOM Style ใช้สำหรับดึงหรือกำหนดการแปลงข้อความขององค์ประกอบในเอกสาร HTML โดยคุณสมบัตินี้จะควบคุมการแสดงผลตัวอักษร เช่น การแปลงเป็นตัวพิมพ์ใหญ่ ตัวพิมพ์เล็ก หรือตัวแรกของแต่ละคำเป็นตัวพิมพ์ใหญ่
ไวยากรณ์ (Syntax)
การดึงค่า textTransform
object.style.textTransform
การกำหนดค่า textTransform
object.style.textTransform = "value"
ค่าที่รองรับ (Values)
| ค่า | คำอธิบาย |
|---|---|
| inherit | สืบทอดค่าคุณสมบัตินี้จากองค์ประกอบผู้ปกครอง (parent element) |
| initial | กำหนดค่าคุณสมบัติเป็นค่าเริ่มต้น (default value) |
| none | ไม่มีการแปลงข้อความ (ค่าเริ่มต้น) |
| uppercase | แปลงตัวอักษรทั้งหมดเป็นตัวพิมพ์ใหญ่ |
| lowercase | แปลงตัวอักษรทั้งหมดเป็นตัวพิมพ์เล็ก |
| capitalize | แปลงตัวอักษรแรกของแต่ละคำเป็นตัวพิมพ์ใหญ่ |
ตัวอย่างการใช้งาน
ตัวอย่างด้านล่างแสดงการใช้คุณสมบัติ textTransform เพื่อเปลี่ยนข้อความในแท็ก <p> เป็นตัวพิมพ์ใหญ่เมื่อคลิกปุ่ม:
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #000;
background: lightblue;
height: 100vh;
}
p {
margin: 1.5rem auto;
}
.btn {
background: #db133a;
border: none;
height: 2rem;
border-radius: 2px;
width: 40%;
display: block;
color: #fff;
outline: none;
cursor: pointer;
}
</style>
</head>
<body>
<h1>ตัวอย่างคุณสมบัติ DOM Style textTransform</h1>
<p>Hi! I'm a paragraph element with some dummy text.</p>
<button onclick="add()" class="btn">Change textTransform</button>
<script>
function add() {
document.querySelector('p').style.textTransform = "uppercase";
}
</script>
</body>
</html>
ผลลัพธ์ (Output)
เมื่อโหลดหน้าเว็บ จะแสดงข้อความปกติในแท็ก <p> และเมื่อคลิกปุ่ม Change textTransform ข้อความจะถูกแปลงเป็นตัวพิมพ์ใหญ่ทั้งหมด