问答详情
源自:9-3 getElementsByTagName()方法

name为sex的节点明明只有两个,为什么弹出来是3?

<!DOCTYPE HTML>
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
<title>JavaScript</title>  
</head>  
<body>  
    
        <form name="Input">
            <table align="center" width="500px" height="50%" border="1">
                <tr>
                    <td align="center" width="100px">
                        学号:
                    </td>
                    <td align="center" width="300px">
                        <input type="text" id=userid name="user" onblur="validate();">
                        <div id=usermsg></div>
                    </td>
                </tr>
                <tr>
                    <td align="center" width="100px">
                        姓名:
                    </td>
                        <td align="center">
                        <input type="text" name="name">
                    </td>
                </tr>
                <tr>
                    <td align="center" width="%45">
                        性别:
                    </td>
                    <td align="center">
                        <input type="radio" name="sex" value="男">
                        男
                        <input type="radio" name="sex" value="女">
                        女
                    </td>
                </tr>
                <tr>
                    <td align="center" width="30%">
                        年龄:
                    </td>
                    <td align="center" width="300px">
                        <input type="text" name="age">
                    </td>
                </tr>
                <tr>
                    <td align="center" width="100px">
                        地址:
                    </td>
                    <td align="center" width="300px">
                        <input type="text" name="addr">
                    </td>
                </tr>

            </table>
        </form>
        <h1 id="myHead" onclick="getValue()">
            看看三种获取节点的方法?
        </h1>
        <p>
            点击标题弹出它的值。
        </p>
        <input type="button" onclick="getElements()"
            value="看看name为sex的节点有几个?" />
        <Br>
        <input type="button" onclick="getTagElements()"
            value="看看标签名为input的节点有几个?" />
            
     <script type="text/javascript">
         function getValue()
          {
              var myH=document.getElementById("myHead");
              alert(myH.innerHTML)
          }
          function getElements()
          {
             var myS=document.getElementsByName="sex";
              alert(myS.length);
          }

          function getTagElements()
          {
              var myI=document.getElementsByTagName('input');
              alert(myI.length);
          }
         
     </script>        

    </body>
</html>

54bf0b0e000155f803970103.jpg

54bf0b0e0001e5d303750098.jpg

54bf0b2d0001e06b04200198.jpg


提问者:网页设计学习者 2015-01-21 10:13

个回答

  • shuaiagain
    2015-01-21 10:27:44
    已采纳

    写错了你,

    这是你写的:

       function getElements()

              {

                 var myS=document.getElementsByName="sex";

                  alert(myS.length);

              }

     

    应该写成:

     function getElements()

              {

                 var myS=document.getElementsByName("sex");

                  alert(myS.length);

              }

    之所以会输出3是因为,你输出的是myS="sex"

    这个字符串的长度