HTML DOM 문서 스크립트
예
문서의 <script> 요소 수:
document.scripts.length;
첫 번째 <script> 요소의 내용을 반환합니다.
document.scripts[0].text;
첫 번째 <script> 요소의 내용을 반환합니다.
document.scripts.item(0).text;
아래에 더 많은 예가 있습니다.
정의 및 사용
이 scripts
속성은 문서에 있는 모든 <script> 요소의 컬렉션을 반환합니다.
scripts
속성은 HTMLCollection 을 반환합니다 .
속성 은 scripts
읽기 전용입니다.
또한보십시오:
스크립트 개체 .
HTML컬렉션
HTMLCollection 은 HTML 노드의 모음입니다 .
컬렉션의 노드는 인덱스 번호로 액세스할 수 있습니다. 인덱스는 0에서 시작합니다.
length 속성 은 컬렉션의 요소 수를 반환합니다.
통사론
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> 요소. 요소는 문서에 나타나는 대로 정렬됩니다. |
더 많은 예
id="myScript"인 <script> 요소의 내용을 반환합니다.
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 |