DOM Node對象lookupNamespaceURI()方法

DOM Node對象lookupNamespaceURI()方法從當前節點開始獲取與名稱空間首碼關聯的名稱空間的URI。

語法

以下是使用lookupNamespaceURI方法的語法。

nodeObject.lookupNamespaceURI(DOMString prefix)

參數

  • prefix - 基於此參數,如果存在任何名稱,則返回uri,如果未找到,則返回null。它是DOMString類型。

返回值

  • 此方法返回關聯的命名空間URI,如果未找到,則返回null

示例

檔:node_ns.xml 的內容如下 -

<?xml version="1.0"?>
<Company>
   <Employee xmlns:e = "http://localhost/technical/" category = "technical">
      <e:FirstName>Max</e:FirstName>
      <e:LastName>Lee</e:LastName>
      <e:ContactNo>1234567890</e:ContactNo>
      <e:Email>Maxlee@xuhuhu.com</e:Email>
   </Employee>

   <Employee xmlns:n = "http://localhost/non-technical/" category = "non-technical">
      <n:FirstName>Tianya</n:FirstName>
      <n:LastName>Su</n:LastName>
      <n:ContactNo>1234667898</n:ContactNo>
      <n:Email>tianya@xuhuhu.com</n:Email>
   </Employee>
</Company>

以下示例演示了lookupNamespaceURI()方法的用法,檔:lookupnamespaceuri.html -

<!DOCTYPE html>
<html>
   <meta charset="utf-8"/>
   <head>
      <script>
         function loadXMLDoc(filename) {
            if (window.XMLHttpRequest) {
               xhttp = new XMLHttpRequest();
            } else{ // code for IE5 and IE6
               xhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xhttp.open("GET",filename,false);
            xhttp.send();
            return xhttp.responseXML;
         }
      </script>
   </head>
   <body>
      <script>
         xmlDoc = loadXMLDoc("/node_ns.xml");

         y = xmlDoc.getElementsByTagName("Employee")[0];
         document.write("lookupNameSpaceURI is : ")
         document.write(y.lookupNamespaceURI("e"));
      </script>
   </body>
</html>

執行上面示例代碼,得到以下結果 -


上一篇: DOM Node對象 下一篇: DOM NodeList對象