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

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

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

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

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

<!DOCTYPE html>
<html>
<head>
  <style>
    [alt$="Connect"] {
      border: 5px solid blue;
      border-radius: 5px;
    }
  </style>
</head>
<body>
  <img src="https://www.tutorialspoint.com/videotutorials/images/tutor_connect_home.jpg" height="200" width="200" alt="Tutor Connect">
  <img src="https://www.tutorialspoint.com/videotutorials/images/tutorial_library_home.jpg" height="200" width="200" alt="Tutorials Library">
</body>
</html>

ผลลัพธ์: รูปแรกมีขอบสีน้ำเงินเพราะ alt ลงท้ายด้วย "Connect" รูปที่สองลงท้ายด้วย "Library" จึงไม่ตรง ตรงข้ามกับ [attribute^="value"] (ขึ้นต้นด้วย) และ [attribute*="value"] (มีอยู่ที่ไหนก็ได้)