HTML - DOM 屬性 isId 屬性



HTML DOM 屬性 isId 屬性用於確定 HTML 屬性是否為“id”屬性。isId 屬性在實現 Attr 介面的物件上可用。

在 HTML 上下文中,id 用於在 HTML 文件中唯一標識元素,從而允許進行目標樣式、指令碼和連結。

isId 屬性已棄用,現代瀏覽器不再支援。如果您嘗試訪問此屬性,瀏覽器將返回“undefined”值。或者,您可以使用 HTML DOM 屬性名稱屬性 獲取屬性的名稱並檢查它是否為“id”

語法

attribute.isId

返回值

它返回布林值。如果存在“id”屬性,則返回 true,否則返回 false。如上所述,在現代瀏覽器中,它將返回“undefined”。

HTML isId DOM 屬性示例

以下是一些示例程式碼,說明如何在 HTML 和 JavaScript 中使用 isId 屬性。

檢查 ID 屬性是否存在

這是一個檢查 div 標籤上是否存在 id 屬性的示例程式碼。element.attributes 訪問 DOM 元素的 attributes 屬性,該屬性是註冊到該元素的所有屬性節點的集合(NamedNodeMap)。而 element.attributes[0] 訪問元素的屬性集合中的第一個屬性節點。

<!DOCTYPE html>
<html>

<head>
    <title>HTML DOM Attribute isId Property</title>
</head>

<body>
    <h3>HTML DOM Attribute isId Property</h3>
    <p>The attribute of Div element: </p>
    <div id="demo"></div>
    <script>
        const ele = document.getElementById("demo");

        // This will select 1st attribute of 'ele' element
        const answer = ele.attributes[0].isId;
        // Pass the name to inside of div tag
        element.innerHTML = answer;
    </script>
    <p>
        The isId property is deprecated, You can't 
        see the value returned by isId property.
    </p>
</body>

</html> 

isId 屬性的替代方法

HTML DOM 屬性“name”可以用作“isId”屬性的替代方法。這將返回所提到的標籤的屬性名稱。以下程式碼顯示瞭如何在 HTML 和 JavaScript 中使用此屬性。

<!DOCTYPE html>
<html>

<head>
    <title>HTML DOM Attribute isId Property</title>
</head>

<body>
    <h3>HTML DOM Attribute isId Property</h3>
    <p>
        The name property of DOM returns 
        the name of an attribute
    </p>
    <div id="demo"></div>
    <script>
        const element = document.getElementById("demo");

        //This will select name of first attribute of div
        let aName = element.attributes[0].name;
        //Pass the name to inside of div tag
        document.getElementById("demo").innerHTML = aName;
    </script>
</body>

</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
isId
廣告