现在的位置: 首页 > web前端 > 正文

javascript中getElements的一个“s”

2019年08月13日 web前端 ⁄ 共 1179字 ⁄ 字号 评论关闭
Code:
  1. <script language="javascript">   
  2.     {   
  3.         function GetInfo()   
  4.         {   
  5.                             var dxml = new ActiveXObject("Microsoft.XMLDOM");   
  6.                dxml.async="false";   
  7.                dxml.load("info2.xml");   
  8.                        var nameNode = dxml.getElementsByTagName("name");   
  9.         var ageNode = dxml.getElementsByTagName("age");   
  10.         var sexNode = dxml.getElementsByTagName("sex");   
  11.         var desNode = dxml.getElementsByTagName("description");   
  12.                
  13.         var str="名称是:"+nameNode(0).firstChild.nodeValue+   
  14.                      "/n年龄是"+ageNode(0).firstChild.nodeValue+   
  15.                      "/n性别是"+sexNode(0).firstChild.nodeValue+   
  16.                      "/n描述是"+desNode(0).firstChild.nodeValue;   
  17.                
  18.         alert(str);   
  19.         }   
  20.     }   
  21.     </script>  

下面是info2.xml内容

Code:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <Info>  
  3. <basic country="china">  
  4. <name num="4">凤姐</name>  
  5. <age>27</age>  
  6. <sex></sex>  
  7. </basic>  
  8. <description>  
  9. 信凤姐,得自信.   
  10. </description>  
  11. </Info>  

自己看了很长时间,用了很多的alert测试,最后不经意间发现了程序运行错误的原因,dxml.getElementsByTagName("name")中的getElementsByTagName()中的Elements丢了一个“s”,看来写代码是一定要认真,骗不了计算机啊.........特此留念,谨记之。

抱歉!评论已关闭.