HTML DOM 脚本集合
HTML DOM 脚本集合返回 HTML 文档中所有 <script> 元素的集合。
语法
以下是语法 −
document.scripts
脚本对象的属性
| 属性 | 说明 |
|---|---|
| 长度 | 它返回 HTML 文档中集合中 <script> 元素的数量。 |
脚本对象的方法
| 方法 | 说明 |
|---|---|
| [索引] | 它从集合中返回指定的索引 <script> 元素。 |
| 项(索引) | 它从集合中返回指定的索引 <script> 元素。 |
| 命名项(ID) | 它从集合中返回指定的 ID <script> 元素。 |
示例
让我们看一个脚本集合的示例 −
<!DOCTYPE html>
<html>
<head>
<style>
html{
height:100%;
}
body{
text-align:center;
color:#fff;
background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat;
height:100%;
}
p{
font-weight:700;
font-size:1.2rem;
}
ul{
list-style-type: none;
padding:0;
}
.btn{
background:#0197F6;
border:none;
height:2rem;
border-radius:2px;
width:50%;
margin:2rem auto;
display:block;
color:#fff;
outline:none;
cursor:pointer;
}
.show{
font-size:1.5rem;
font-weight:bold;
}
</style>
</head>
<body>
<script>
console.log("script one");
</script>
<h1>DOM scripts Collection Demo</h1>
<p>Hi, There are two script elements on this page:</p>
<script>
console.log("script two");
</script>
<button onclick="show()" class="btn">Show No. Of Script</button>
<div class='show'></div>
<script>
function show() {
var scriptList = document.scripts;
document.querySelector(".show").innerHTML=scriptList.length;
}
</script>
</body>
</html>输出
这将产生以下输出 −

单击“显示脚本数”按钮以显示脚本元素的数量。

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP