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

วิธีสร้าง Conditional Comments ใน HTML สำหรับ Internet Explorer

Conditional Comments เป็นฟีเจอร์เฉพาะของ Internet Explorer (IE 5-9) ใช้สำหรับโหลดโค้ด/สไตล์เฉพาะ IE ไม่ส่งผลต่อ browser อื่น

รูปแบบ

<!--[if IE 9]>
  <link rel="stylesheet" href="ie9.css">
<![endif]-->

เงื่อนไขที่ใช้ได้

เงื่อนไขความหมาย
IEทุกเวอร์ชัน IE
IE 9เฉพาะ IE 9
gt IE 8มากกว่า IE 8 (IE 9+)
gte IE 8มากกว่าหรือเท่ากับ IE 8
lt IE 9น้อยกว่า IE 9
lte IE 7น้อยกว่าหรือเท่ากับ IE 7
!IEไม่ใช่ IE (browser อื่น)

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
  <title>HTML Conditional Comments</title>
  <!--[if IE 9]>
    <style> body { color: red; } </style>
  <![endif]-->
</head>
<body>
  <p>This is demo content.</p>
</body>
</html>

สถานะปัจจุบัน

IE 10+ และ Edge ไม่รองรับ conditional comments แล้ว ใช้ feature detection (Modernizr) หรือ CSS hacks แทน หากไม่ต้องรองรับ IE เก่า ควรไม่ใช้