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对象