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