HTML - DOM 文件 embeds 屬性



HTML DOM 文件的 **embeds** 是一個只讀屬性,用於返回 HTML 文件中所有 **<embed>** 元素作為一個集合。

語法

document.embeds;

屬性

屬性 描述
length 它返回 HTML 文件中存在的 <embed> 元素的數量。

方法

下表顯示了 DOM embeds 提供的方法列表。

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

返回值

它返回一個 HTMLCollection,其中列出了文件中存在的所有 <embed> 元素。集合中的元素已排序,並按其在 HTML 文件中出現的順序顯示。

HTML DOM 文件 'embeds' 屬性示例

以下示例說明了 embeds 屬性和方法的使用。

獲取嵌入元素的數量

在以下示例中,**length** 屬性用於返回文件中 <embed> 元素的數量。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOM document embeds Property</title>
</head>
<body>
    <embed src="/html/images/test.png" type="image/jpg">
    <embed src="/plsql/images/plsql-mini-logo.jpg" type="image/jpg">
    <embed src="/html/images/html.jpg" type="image/jpg">
    <p>Click to get the number of <embed> element.</p>
    <button onclick="fun()">Click me</button>
    <p id="embeds"></p>
    <script>
        function fun() {
            let x = document.embeds.length;
            document.getElementById("embeds").innerHTML = x;
        }
    </script>
</body>
</html>

獲取第一個嵌入元素的 URL

在此示例中,我們將使用 embeds **[索引]** 方法獲取第一個嵌入檔案的 URL。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOM document embeds Property</title>
</head>
<body>
    <embed src="/html/images/test.png" type="image/jpg">
    <embed src="/plsql/images/plsql-mini-logo.jpg" type="image/jpg">
    <embed src="/html/images/html.jpg" type="image/jpg">
    <p>Click to get URL of the first embedded file</p>
    <button onclick="fun()">Click me</button>
    <p id="embeds"></p>
    <script>
        function fun() {
            let x = document.embeds[0].src;
            document.getElementById("embeds").innerHTML = x;
        }
    </script>
</body>
</html>

獲取任何嵌入元素的 URL

在此示例中,我們將使用 embeds **item(索引)** 方法獲取第二個嵌入檔案的 URL。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOM document embeds Property</title>
</head>
<body>
    <embed src="/html/images/test.png" type="image/jpg">
    <embed src="/plsql/images/plsql-mini-logo.jpg" type="image/jpg">
    <embed src="/html/images/html.jpg" type="image/jpg">
    <p>Click to get URL of the first embedded file</p>
    <button onclick="fun()">Click me</button>
    <p id="embeds"></p>
    <script>
        function fun() {
            let x = document.embeds.item(1).src;
            document.getElementById("embeds").innerHTML = x;
        }
    </script>
</body>
</html>   

獲取嵌入元素的類名

在此示例中,我們將使用 embeds **namedItem(id)** 方法獲取第三個嵌入檔案的類名。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOM document embeds Property</title>
</head>
<body>
    <embed src="/html/images/test.png" type="image/jpg">
    <embed src="/plsql/images/plsql-mini-logo.jpg" type="image/jpg">
    <embed src="/html/images/html.jpg" type="image/jpg" id="id3" class="js">
    <p>Click to get class name of the third embedded file</p>
    <button onclick="fun()">Click me</button>
    <p id="embeds"></p>
    <script>
        function fun() {
            let x = document.embeds.namedItem("id3").className;
            document.getElementById("embeds").innerHTML = x;
        }
    </script>
</body>
</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
embeds 是 64 是 12 是 1 是 10.1 是 51
html_dom_document_reference.htm
廣告