สคริปต์เอกสาร HTML DOM
ตัวอย่าง
จำนวน <script> องค์ประกอบในเอกสาร:
document.scripts.length;
ส่งคืนเนื้อหาขององค์ประกอบ <script> แรก:
document.scripts[0].text;
ส่งคืนเนื้อหาขององค์ประกอบ <script> แรก:
document.scripts.item(0).text;
ตัวอย่างเพิ่มเติมด้านล่าง
ความหมายและการใช้งาน
คุณสมบัติscripts
ส่งคืนคอลเลกชันขององค์ประกอบ <script> ทั้งหมดในเอกสาร
คุณสมบัติส่งscripts
คืนHTMLCollection
คุณสมบัติscripts
เป็นแบบอ่านอย่างเดียว
ดูสิ่งนี้ด้วย:
ออบเจ็กต์สคริปต์
HTMLCollection
HTMLCollection คือ ชุดของโหนด HTML
โหนดในคอลเล็กชันสามารถเข้าถึงได้ด้วยหมายเลขดัชนี ดัชนีเริ่มต้นที่ 0
คุณสมบัติความยาวส่งคืนจำนวนองค์ประกอบในคอลเลกชัน
ไวยากรณ์
document.scripts
คุณสมบัติ
คุณสมบัติ | คำอธิบาย |
ระยะเวลา | จำนวนองค์ประกอบ <script> ในคอลเล็กชัน |
วิธีการ
Method | Description |
[index] | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
item(index) | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
namedItem(id) | Returns the element with the specified id. Returns null if the id does not exist. |
คืนมูลค่า
พิมพ์ | คำอธิบาย |
วัตถุ | วัตถุ HTMLCollection องค์ประกอบ <script> ทั้งหมดในเอกสาร องค์ประกอบจะถูกจัดเรียงตามที่ปรากฏในเอกสาร |
ตัวอย่างเพิ่มเติม
ส่งคืนเนื้อหาขององค์ประกอบ <script> ด้วย id="myScript":
document.scripts.namedItem("myScript").text;
วนรอบองค์ประกอบ <script> ทั้งหมดและส่งออกแต่ละ id:
const collection = document.scripts;
let text = "";
for (let i = 0; i < collection.length; i++) {
text += collection[i].id + "<br>";
}
รองรับเบราว์เซอร์
document.scripts
เป็นคุณลักษณะ DOM ระดับ 2 (2001)
ได้รับการสนับสนุนอย่างเต็มที่ในเบราว์เซอร์ทั้งหมด:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |