
Document 对象
查看文档中有多少个 <script> 元素:
var x = document.scripts.length;
scripts 集合返回文档中所有 <script> 元素的集合。
注意: 元素在集合中的排序是它们在源代码中的顺序。
提示: 相关内容 Script
对象。
表格中的数字表示支持该集合的第一个浏览器的版本号。
集合
scripts
Yes
9.0
属性
描述
length
返回集合中 <script> 元素的个数。
提示: 这是一个只读属性。
方法
[index]
返回集合中指定索引(从 0 开始)的 <script> 元素。
注意: 如果索引值超出范围返回 null。
item(index)
namedItem(id)
回集合中指定 id 的 <script> 元素。
注意: 如果 id 不存在返回 null。
DOM 版本:
Core Level 3 Document Object
返回值:
一个 HTMLCollection 对象, 表示文档中所有的 <script> 元素。集合中元素的排序是根据源码中的顺序排列的。
获取文档中第一个(索引为 0) <script> 元素的内容:
var x = document.scripts[0].text;
var x = document.scripts.item(0).text;
获取 id="runoob" 的 <script> 元素的内容:
var x = document.scripts.namedItem("runoob").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> 标签
