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

การทำงานกับ document.embeds ใน JavaScript

Property document.embeds คืนค่า HTMLCollection ของ Element <embed> ทั้งหมดในเอกสาร ใช้ .length เพื่อนับจำนวน

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

<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript Example</title>
  </head>
  <body>
    <embed src="/html/yourfile.mid" width="250" height="100" />
    <script>
      var num = document.embeds.length;
      document.write("<br>How many embed tags: " + num);
    </script>
  </body>
</html>

คำอธิบาย

  • document.embeds — Live HTMLCollection ของ <embed> Elements
  • .length — จำนวน Embed Elements
  • ผลลัพธ์: How many embed tags: 1

ข้อมูลเพิ่มเติม

  • <embed> ใช้สำหรับฝัง External Content (Plugin, PDF, Media) — ต่างจาก <object> และ <iframe>
  • เข้าถึง Element เฉพาะ: document.embeds[0] หรือ document.embeds.namedItem('id')
  • รองรับ Browser ทุกตัว รวม IE (แต่ <embed> เองอาจต้อง Plugin)

สมัยใหม่: ควรใช้ <video>, <audio>, <iframe>, หรือ <object> แทน <embed> สำหรับเนื้อหาสื่อสมัยใหม่