小白教程
所有教程
关于
Search
172.70.131.190
172.70.131.190
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Coll doc scripts”的源代码
本页内容
上一节:
Met_document_renamenode
下一节:
Prop_document_stricterrorchecking
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:document.scripts}}[[Category:dom obj document|40]] = HTML DOM scripts 集合 = [[dom obj document|Document 对象]] 查看文档中有多少个 <script> 元素: <sample title="" desc="" lang="html" hererun="1"> var x = document.scripts.length; </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <script> document.write("Hello xiaoBai!"); </script> <p>点击按钮查看文档中有多少个 script 元素。</p> <button onclick="myFunction()">点我</button> <p id="demo"></p> <script> function myFunction() { var x = document.scripts.length; document.getElementById("demo").innerHTML = "找到 " + x + " 个 script 元素。"; } </script> </body> </html> </run> == 定义与用法 == scripts 集合返回文档中所有 <script> 元素的集合。 '''注意:''' 元素在集合中的排序是它们在源代码中的顺序。 '''提示:''' 相关内容 [[dom obj script|Script 对象]]。 == 浏览器支持 == 表格中的数字表示支持该集合的第一个浏览器的版本号。 {| class="table table-striped table-hover" ! 集合 ! Chrome ! Internet Explorer / Edge ! Firefox ! Safari ! Opera |- | scripts | Yes | Yes | 9.0 | Yes | Yes |} == 语法 == <pre> document.scripts </pre> == 属性 == {| class="table table-striped table-hover" ! 属性 ! 描述 |- | length | 返回集合中 <script> 元素的个数。 '''提示:''' 这是一个只读属性。 |} == 方法 == {| class="table table-striped table-hover" ! 方法 ! 描述 |- | [index] | 返回集合中指定索引(从 0 开始)的 <script> 元素。'''注意:''' 如果索引值超出范围返回 null。 |- | item(index) | 返回集合中指定索引(从 0 开始)的 <script> 元素。'''注意:''' 如果索引值超出范围返回 null。 |- | namedItem(id) | 回集合中指定 id 的 <script> 元素。 '''注意:''' 如果 id 不存在返回 null。 |} == 技术细节 == {| class="table table-striped table-hover" | DOM 版本: | Core Level 3 Document Object |- | 返回值: | 一个 HTMLCollection 对象, 表示文档中所有的 <script> 元素。集合中元素的排序是根据源码中的顺序排列的。 |} == 更多实例 == [index] 获取文档中第一个(索引为 0) <script> 元素的内容: <sample title="" desc="" lang="html" hererun="1"> var x = document.scripts[0].text; </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <script> document.write("Hello xiaoBai!"); </script> <p>点击按钮获取文档中第一个(索引为 0) script 元素的内容。</p> <button onclick="myFunction()">点我</button> <p id="demo"></p> <script> function myFunction() { var x = document.scripts[0].text; document.getElementById("demo").innerHTML = x; } </script> </body> </html> </run> item(index) 获取文档中第一个(索引为 0) <script> 元素的内容: <sample title="" desc="" lang="html" hererun="1"> var x = document.scripts.item(0).text; </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <script> document.write("Hello xiaoBai!"); </script> <p>点击按钮获取文档中第一个(索引为 0) script 元素的内容。</p> <button onclick="myFunction()">点我</button> <p id="demo"></p> <script> function myFunction() { var x = document.scripts.item(0).text; document.getElementById("demo").innerHTML = x; } </script> </body> </html> </run> namedItem(id) 获取 id="xiaobai" 的 <script> 元素的内容: <sample title="" desc="" lang="html" hererun="1"> var x = document.scripts.namedItem("xiaobai").text; </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <script> document.write("Hello xiaoBai!"); </script> <p>点击按钮获取 id="xiaobai" 的 script 元素内容。</p> <button onclick="myFunction()">点我</button> <p id="demo"></p> <script id="xiaobai"> function myFunction() { var x = document.scripts.namedItem("xiaobai").text; document.getElementById("demo").innerHTML = x; } </script> </body> </html> </run> 遍历文档中所有的 <script> 元素,并输出每个 <script> 元素的 id: <sample title="" desc="" lang="html" hererun="1"> var x = document.scripts; var txt = ""; var i; for (i = 0; i < x.length; i++) { txt = txt + x[i].id + "<br>"; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <script id="xiaobai1"> document.write("Hello xiaoBai!"); </script> <p>点击按钮遍历文档中所有的 script 元素,并输出每个 script 元素的 id:。</p> <button onclick="myFunction()">点我</button> <p id="demo"></p> <script id="xiaobai2"> function myFunction() { var x = document.scripts; var txt = ""; var i; for (i = 0; i < x.length; i++) { txt = txt + x[i].id + "<br>"; } document.getElementById("demo").innerHTML = txt; } </script> </body> </html> </run> == 相关文章 == JavaScript 参考手册: [[dom obj script|HTML DOM Script 对象]] HTML 教程: [[html scripts|HTML 脚本]] HTML 参考手册: [[tag script|HTML <script> 标签]] [[dom obj document|Document 对象]]
返回至“
Coll doc scripts
”。
上一节:
Met_document_renamenode
下一节:
Prop_document_stricterrorchecking