DOM Element对象getAttribute()方法用于获取属性的值(如果属性存在于指定元素)。
语法
以下是getAttribute()方法的使用语法。
elementObj.getAttribute(name)
参数
name- 它包含要检索的属性的名称。
返回值
- 如果存在,此方法将值作为字符串返回,否则将指定为
null。 
示例
文件:node.xml 的内容如下 -
<?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>
<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>
以下示例演示了getAttribute()方法的用法 - 
<!DOCTYPE html>
<html>
   <meta charset="utf-8"/>
   <head>
   </head>
   <body>
      <script>
         if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
         } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.open("GET","/node.xml",false);
         xmlhttp.send();
         xmlDoc = xmlhttp.responseXML;
         x = xmlDoc.getElementsByTagName('Employee')[2];
         document.write("属性是: ");
         document.write(x.getAttribute('category'));
      </script>
   </body>
</html>
执行上面示例代码,得到以下结果 -

						上一篇:
								DOM Element对象
												下一篇:
								DOM Attribute对象
												
						
						
					
					
					