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

วิธีดึงค่า type attribute ของลิงก์ใน JavaScript

ใช้ Property .type เพื่อดึงค่า type attribute ของลิงก์ (<a>) ซึ่งระบุ MIME Type ของเอกสารปลายทาง เช่น text/html, text/css, application/pdf

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

<!DOCTYPE html>
<html>
  <body>
    <p><a id="anchorid" type="text/html" target="_blank" href="https://www.qries.com/">Qries</a></p>
    <script>
      var myVal = document.getElementById("anchorid").type;
      document.write("Value of type attribute: " + myVal);
    </script>
  </body>
</html>

คำอธิบาย

  • Property .type คืนค่า String ของ MIME Type
  • ผลลัพธ์: Value of type attribute: text/html
  • หากไม่ระบุ type attribute จะคืนค่า Empty String ""

หมายเหตุ: Browser ไม่บังคับใช้ค่านี้ แต่อาจใช้สำหรับ CSS Selector หรือ JavaScript Logic