HTML DOM scripts 集合

Document 對象參考手冊 Document 對象

實例

查看文檔中有多少個 <script> 元素:

var x = document.scripts.length;


定義與用法

scripts 集合返回文檔中所有 <script> 元素的集合。

注意: 元素在集合中的排序是它們在源代碼中的順序。

提示: 相關內容 Script 對象


流覽器支持

表格中的數字表示支持該集合的第一個流覽器的版本號。

集合
scripts Yes Yes 9.0  Yes Yes

語法

document.scripts

屬性

屬性 描述
length 返回集合中 <script> 元素的個數。

提示: 這是一個只讀屬性。

方法

方法 描述
[index] 返回集合中指定索引(從 0 開始)的 <script> 元素。

注意: 如果索引值超出範圍返回 null。
item(index) 返回集合中指定索引(從 0 開始)的 <script> 元素。

注意: 如果索引值超出範圍返回 null。
namedItem(id) 回集合中指定 id 的 <script> 元素。

注意: 如果 id 不存在返回 null。

技術細節

DOM 版本: Core Level 3 Document Object
返回值: 一個 HTMLCollection 對象, 表示文檔中所有的 <script> 元素。集合中元素的排序是根據源碼中的順序排列的。

更多實例

實例

[index]

獲取文檔中第一個(索引為 0) <script> 元素的內容:

var x = document.scripts[0].text;

實例

item(index)

獲取文檔中第一個(索引為 0) <script> 元素的內容:

var x = document.scripts.item(0).text;

實例

namedItem(id)

獲取 id="zaixian" 的 <script> 元素的內容:

var x = document.scripts.namedItem("zaixian").text;

實例

遍曆文檔中所有的 <script> 元素,並輸出每個 <script> 元素的 id:

var x = document.scripts; var txt = ""; var i; for (i = 0; i < x.length; i++) { txt = txt + x[i].id + "<br>"; }


相關文章

JavaScript 參考手冊: HTML DOM Script 對象

HTML 教學: HTML 腳本

HTML 參考手冊: HTML <script> 標籤


Document 對象參考手冊 Document 對象