HTML - DOM 文件 applets 屬性



HTML DOM 文件的 **applets** 屬性用於返回文件中所有 applet 元素的列表,但此屬性現已 **棄用**。它在所有新的網路瀏覽器中返回一個空的 HTMLCollection。

applets 集合的 **length** 屬性返回 HTML 文件中 applet 元素的數量。您可以使用替代方法 getElementsByTagName('applet') 來獲取相同的結果。

語法

document.applets;

返回值

它返回一個 HTMLCollection 物件,其中列出了文件中存在的所有 <applet> 元素。

方法

下表顯示了 DOM applets 集合提供的方法列表。

方法 描述
[索引] 從給定索引處的集合中返回 <applet> 元素。索引從 0 開始,如果索引超出範圍,則返回 null。
item(索引) 從給定索引處的集合中返回 <applet> 元素。索引從 0 開始,如果索引超出範圍,則返回 null。它與第一種方法類似。
namedItem(id) 從具有給定 id 的集合中返回 <applet> 元素。如果 id 不存在,則返回 null。

HTML DOM 文件“applets”屬性示例

以下示例說明了 appplets 屬性的使用。

獲取 applet 元素的數量

以下示例說明了如何獲取文件中 applet 元素的數量。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOM document applets Property</title>
</head>
<body>
    <p>Click to get the number of applet element</p>
    <button onclick="fun()">Click me</button>
    <p id="applets"></p>
    <script>
        function fun() {
            let x = document.applets.length;
            document.getElementById("applets").innerHTML = x;
        }
    </script>
</body>
</html>

獲取 applet 元素數量的替代方法

在此示例中,getElementsByTagName 用作獲取 <applet> 元素數量的替代方法

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOM document applets Property</title>
</head>
<body>
    <applet></applet>
    <p>Click to get the number of applet element</p>
    <button onclick="fun()">Click me</button>
    <p id="applets"></p>
    <script>
        function fun() {
            let num = document.getElementsByTagName("applet").length;
            document.getElementById("applets").innerHTML = num;
        }
    </script>
</body>
</html>

獲取第一個 applet 元素的內容

在此示例中,我們將使用 applets 的 **[索引]** 方法獲取第一個 <applet> 的內容。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOM document applets Property</title>
</head>
<body>
    <applet>This is just to show the content of applet tag</applet>
    <p>Click to get the number of applet element</p>
    <button onclick="fun()">Click me</button>
    <p id="applets"></p>
    <script>
        function fun() {
            let num = document.getElementsByTagName("applet")[0].innerHTML;
            document.getElementById("applets").innerHTML = num;
        }
    </script>
</body>
</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
applets
廣告