document.getElementById('Button').onclick = validata;
function validata()
{
           var str=document.getElementById('Uname').value;
if (str == "")
{
alert('yourName!!!!');
document.getElementById('Uname').focus();
}
else
{
alert("well com !!");
}
        }<form id="myForm" name="myForm" >
Name : <input type="text" id="Uname" size="30" />
    <input type="button" id="Button" name="Button" value="button" />
</form>document.getElementById('Button').onclick = validata; 这条语句应该怎么理解啊。
为什么当 Button click 时候不去执行 validata() 啊。

解决方案 »

  1.   

    document.getElementById('Button').onclick = validata;这有什么意义吗?document.getElementById('Button').onclick = validata();是不是要实现这个意思啊
      

  2.   

    document.getElementById('Button').onclick = validata();
    Button  click 时候  validata()还是没有执行。
      

  3.   

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script>
            function validata() {
                var str = document.getElementById('Uname').value;
                if (str == "") {
                    alert('yourName!!!!');
                    document.getElementById('Uname').focus();
                }
                else {
                    alert("well com !!");
                }
            }    </script>
    </head>
    <body>
    <form id="myForm" name="myForm" >
     Name : <input type="text" id="Uname" size="30" />
       <input type="button" id="Button" name="Button" value="button" onclick="validata()" />
    </form></body>
    </html>
    是不是想要这种效果啊
      

  4.   

    onclick="validata()"
    我们 tl 说可以不用在  HTML 标签里 用onclick 可以实现。
      

  5.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head><body>
    <form id="myForm" name="myForm" >
    Name : <input type="text" id="Uname" size="30" />
      <input type="button" id="Button" name="Button" value="button" />
    </form>
    <script>
    document.getElementById('Button').onclick = validata;
    function validata()
    {
      var str=document.getElementById('Uname').value;
    if (str == "")
    {
    alert('yourName!!!!');
    document.getElementById('Uname').focus();
    }
    else
    {
    alert("well com !!");
    }
      }
    </script>
    </body>
    </html>
    把js代码放到最下面~·
      

  6.   

    <form id="myForm" name="myForm" >
    Name : <input type="text" id="Uname" size="30" />
      <input type="button" id="Button" name="Button" value="button" />
    </form>
    <script type="text/javascript">
    document.getElementById('Button').onclick = validata; //给id为Button的元素 加onclick事件
    function validata()
    {
    var str = document.getElementById('Uname').value;
    if (str == "")
    {
    alert('yourName!!!!');
    document.getElementById('Uname').focus();
    }
    else
    {
    alert("well com !!");
    }
    }
    </script>
      

  7.   

    下边这么写就是在html里边的
    function validata()
    {
      var str=document.getElementById('Uname').value;
    if (str == "")
    {
    alert('yourName!!!!');
    document.getElementById('Uname').focus();
    }
    else
    {
    alert("well com !!");
    }
      }<form id="myForm" name="myForm" >
    Name : <input type="text" id="Uname" size="30" />
    <input type="button" id="Button" name="Button" value="button" onclick="validata()"/>
    </form>
      

  8.   


    document.getElementById('Button').onclick = function()
    {
      var str=document.getElementById('Uname').value;
      if (str == "")//提个建议,楼主这儿验证值得注意,如果我单纯输入空格怎么办?算"合理"?
    {
    alert('yourName!!!!');
    document.getElementById('Uname').focus();
    }
    else
    {
    alert("well com !!");
    }
      

  9.   

    if (str == "")//提个建议,楼主这儿验证值得注意,如果我单纯输入空格怎么办?算"合理"?
    里面详细代码我没写出来那些正则表达式什么的,这些代码只是一个测试用的。