HTML - DOM 元素 isDefaultNamespace() 方法



**isDefaultNamespace()** 方法检查特定的命名空间 URI 是否是文档或元素中元素的默认命名空间。

**URI:**用于标识互联网或其他地方资源的字符字符串。

**URI** 代表统一资源标识符。

语法

element.isDefaultNamespace(namespaceURI)

参数

方法 描述
namespaceURI 是一个指定要检查的命名空间 URI 的字符串。

返回值

如果 namespaceURI 是文档中元素的默认命名空间,则此方法返回“true”。否则,返回“false”。

HTML - DOM 元素“isDefaultNamespace()”的示例

以下是一些示例,说明如何使用 isDefaultNamespace() 方法来检查命名空间 URI 是否为元素的默认命名空间。

检查默认命名空间

此示例使用**<div>**元素设置默认命名空间,然后脚本检查提到的 div 元素是否为 exampleDiv 元素的默认命名空间。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>isDefaultNamespace Example</title>
</head>

<body>
    <h1>HTML - DOM Element</h1>
    <h2>isDefaultNamespace() method</h2>
    <p>Checking default namespace...</p>
    
    <div id="exampleDiv"
        xmlns="http://www.w3.org/1999/xhtml">
        Is this div in the default namespace?
    </div>

    <p id="resultMessage"></p>
    
    <script>
        const exampleDiv = document.getElementById
        ('exampleDiv');
        const resultMessage = 
        exampleDiv.isDefaultNamespace
        ("http://www.w3.org/1999/xhtml");

        document.getElementById
        ('resultMessage').innerText = resultMessage;
    </script>
</body>

</html>

非默认命名空间

此示例使用一个声明为 custom 的自定义命名空间的 <div> 元素,然后脚本检查提到的 div 元素是否为 exampleDiv 元素的默认命名空间。

<!DOCTYPE html>
<html lang="en">
<head>  
    <title>isDefaultNamespace Example</title>
</head>

<body>
    <h1>HTML - DOM Element</h1>
    <h2>isDefaultNamespace method</h2>
    <p>Checking default namespace...</p>
    
    <div id="exampleDiv"xmlns="http://www.example.com/ns">
        Is this div in the default namespace?
    </div>

    <p id="resultMessage"></p>
    
    <script>
        const exampleDiv = 
        document.getElementById('exampleDiv');
        const resultMessage = 
        exampleDiv.isDefaultNamespace(null);  
        document.getElementById
        ('resultMessage').innerText = resultMessage;
    </script>    
</body>

</html>
        

支持的浏览器

方法 Chrome Edge Firefox Safari Opera
isDefaultNamespace()
html_dom_element_reference.htm
广告

© . All rights reserved.