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

คุณสมบัติ hreflang ของ Link ใน HTML DOM

คุณสมบัติ hreflang ของอีลีเมนต์ <link> ใช้ตั้งค่าหรืออ่านรหัสภาษาของเอกสารที่ลิงก์ไป

ไวยากรณ์ (Syntax)

การอ่านค่า

linkObject.hreflang

การตั้งค่า

linkObject.hreflang = langCode

ตัวอย่างการใช้งาน

ตัวอย่างด้านล่างตรวจสอบภาษาของ stylesheet ที่ลิงก์:

<!DOCTYPE html>
<html>
<head>
<title>Link hreflang</title>
<link id="extStyle" hreflang='en' rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form>
  <fieldset>
    <legend>Link-hreflang</legend>
    <h3>Spanish</h3>
    <input type="button" onclick="changeStyle()" value="In what language is linked document written?">
    <div id="divDisplay"></div>
  </fieldset>
</form>
<script>
  var divDisplay = document.getElementById("divDisplay");
  var extStyle = document.getElementById("extStyle");
  
  function changeStyle() {
    if(extStyle.hreflang === 'en'){
      divDisplay.textContent = 'The linked document is written in english';
      document.getElementsByTagName('h3')[0].textContent = 'English';
    }
  }
</script>
</body>
</html>

ในตัวอย่าง style.css มีเนื้อหา:

form { width:70%; margin: 0 auto; text-align: center; }
* { padding: 2px; margin:5px; }
input[type="button"] { border-radius: 10px; }
h3 { font-size: 72px; color:gold; }

ผลลัพธ์

ก่อนคลิกปุ่ม: แสดงหัวข้อ "Spanish"

หลังคลิกปุ่ม: หัวข้อเปลี่ยนเป็น "English", แสดง "The linked document is written in english"

หมายเหตุ: รูปภาพตัวอย่างจากต้นฉบับไม่ได้รวมอยู่ในเนื้อหานี้