DOM NamedNodeMap对象setNamedItem()方法使用nodeName属性添加节点。 如果此映射中已存在具有该名称的节点,则该节点将替换为新映射。
语法
以下是setNamedItem()方法的使用语法。
nodemapObject.setNamedItem(arg)
参数
arg- 这将节点存储在映射中,之后可以使用nodeName属性访问此节点值。 它是类型节点。
返回值
- 如果替换现有节点,则此方法返回节点的新更新值,否则返回
null。 
示例
文件:node.xml 的内容如下 -
<Company> 
   <Employee category = "Technical" id = "firstelement"> 
      <FirstName>Susen</FirstName> 
      <LastName>Su</LastName> 
      <ContactNo>1584567890</ContactNo> 
      <Email>susen@xuhuhu.com</Email> 
   </Employee>  
   <Employee category = "Non-Technical"> 
      <FirstName>Max</FirstName> 
      <LastName>Su</LastName> 
      <ContactNo>1334667898</ContactNo> 
      <Email>maxsu@xuhuhu.com</Email> 
   </Employee>  
   <Employee category = "Management"> 
      <FirstName>Min</FirstName> 
      <LastName>Su</LastName> 
      <ContactNo>1364562350</ContactNo> 
      <Email>minsu@xuhuhu.com</Email> 
   </Employee> 
</Company>
以下示例演示了setNamedItem()方法的用法 - 
<!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.xml");
         c = xmlDoc.createAttribute("category");
         c.value = "zaixian zaixian";
         y = xmlDoc.getElementsByTagName('Employee')[0].attributes;
         y.setNamedItem(c);
         document.write("设置名为Item的值为 : ")
         document.write(y.getNamedItem('category').nodeValue);
      </script>
    </body>
</html>
执行上面代码,得到以下结果 -

						上一篇:
								DOM NamedNodeMap对象
												下一篇:
								DOMImplementation对象
												
						
						
					
					
					