怎么点击没效果显示

来源:9-3 getElementsByTagName()方法

蒲蒲森

2017-08-21 14:19

<!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()
          {
              vat 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>

代码应该没错呀,怎么最后点击页面的按钮都没反应

写回答 关注

1回答

  • 七秒飞翔
    2017-08-21 15:55:26
    已采纳

    注意看你的73行代码  vat myH = document.getElementById("myHead");

    出错原因:var你写成来vat,

    你修改成var myH = document.getElementById("myHead");

    代码成功运行。

    蒲蒲森

    居然没发现这里错了!非常感谢!

    2017-08-21 15:57:43

    共 1 条回复 >

JavaScript进阶篇

本课程从如何插入JS代码开始,带您进入网页动态交互世界

468061 学习 · 21891 问题

查看课程

相似问题