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

แอตทริบิวต์เหตุการณ์ ontoggle ใน HTML

แอตทริบิวต์เหตุการณ์ ontoggle ใน HTML ถูกกระตุ้นเมื่อผู้ใช้เปิดหรือปิดองค์ประกอบ <details> HTML ในเอกสาร HTML

ไวยากรณ์

<details ontoggle="script">เนื้อหา</details>

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
<style>
  body { color: #000; height: 100vh; background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat; text-align: center; padding: 20px; }
</style>
</head>
<body>
<h1>ตัวอย่างแอตทริบิวต์เหตุการณ์ ontoggle ของ HTML</h1>
<details ontoggle="toggleFn()">
  <summary>สวัสดี! ฉันคือองค์ประกอบ summary</summary>
  <p>นี่คือพารากราฟพร้อมเนื้อหาตัวอย่าง.</p>
</details>
<script>
  function toggleFn() {
    document.body.style.background = "linear-gradient(45deg, #8BC6EC 0%, #9599E2 100%) no-repeat";
    document.body.style.color = "#fff";
  }
</script>
</body>
</html>

ผลลัพธ์

ลองเปิด/ปิดองค์ประกอบ details เพื่อดูการทำงานของ ontoggle

พื้นหลังจะเปลี่ยนสีเมื่อ toggle details เปิด/ปิด