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

เลือกอิลิเมนต์ที่แอตทริบิวต์มีค่าที่ระบุด้วย CSS

เพื่อเลือกอิลิเมนต์ที่ค่าแอตทริบิวต์มีข้อความที่ระบุอยู่ ให้ใช้ Selector [attribute*="value"]

ตัวอย่างโค้ด

ลองรันโค้ดด้านล่างเพื่อทดสอบ CSS [attribute*="value"] selector:

<!DOCTYPE html>
<html>
<head>
  <style>
    [alt*="tut"] {
      border: 5px solid orange;
      border-radius: 5px;
    }
  </style>
</head>
<body>
  <img src="https://www.tutorialspoint.com/videotutorials/images/tutor_connect_home.jpg" height="200" width="200" alt="tutor">
  <img src="https://www.tutorialspoint.com/videotutorials/images/tutorial_library_home.jpg" height="200" width="200" alt="my tutorials">
</body>
</html>

ผลลัพธ์: รูปทั้งสองจะมีขอบสีส้ม เพราะค่า alt ทั้งคู่มีคำว่า "tut" อยู่ (tutor และ tutorials) Selector นี้ค้นหา substring ไว้ที่ไหนในค่าแอตทริบิวต์ก็ได้