Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

คุณสมบัติ textTransform ของ HTML DOM Style

คุณสมบัติ 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 ข้อความจะถูกแปลงเป็นตัวพิมพ์ใหญ่ทั้งหมด