DOM Node對象lookupPrefix()方法

DOM Node對象lookupPrefix()方法返回命名空間URI的當前名稱空間中定義的最接近的首碼。 如果找到則返回關聯的命名空間首碼,如果沒有找到則返回null

語法

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

nodeObject.lookupPrefix(DOMString namespaceURI)

參數

  • namespaceURI - 基於此參數返回首碼,它是DOMString類型。

返回值

  • 此方法返回關聯的命名空間首碼,如果未找到,則返回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>

以下示例演示了lookupPrefix()方法的用法,檔:lookupprefix.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("lookupPrefix is : ")
         document.write(y.lookupPrefix("http://localhost/technical/"));
      </script>
   </body>
</html>

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


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