按钮未正确调用功能

时间:2016-05-03 06:43:26

标签: javascript html function event-handling

我试图在按钮点击时调用一个函数,但由于某种原因按钮不会调用该函数。 Dreamweaver不显示任何语法错误。任何人都可以告诉我为什么按钮不起作用?

    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta charset="utf-8" />
    </head>
   <head>
   <title></title>


<script type="text/javascript">

        var imgObj = 0;
        var imgObj1 = 0;
        var animate;
        var animate1;

        function init(){
           imgObj = document.getElementById('red');
           imgObj.style.position= 'relative';
           imgObj.style.left = '0px';
           imgObj1 = document.getElementById('blue');
           imgObj1.style.position = 'relative';
           imgObj1.style.left = '0px';
        }

        function moveRight(){
           imgObj.style.left = parseInt(imgObj.style.left = 0) + Math.floor((Math.random() * 100) + 1) + 'px';

           animate = setTimeout(moveRight(), 1000); 
           imgObj1.style.left = parseInt(imgObj1.style.left = 0) + Math.floor((Math.random() * 100) + 1) + 'px';

           animate1 = setTimeout(moveRight(), 1000); 
           if (imgObj.style.left >= 1000px || imgObj1.style.left >= 1000px)
            {
                break;
                else if
                {
                    imgObj.style.left>= 1000
                    MessageBox.show("The Red Car has won the Race!");
                }
                else
                {
                    MessageBox.show("The Blue Car has won the Race!");
                }

            }
        }           
</script>

</head>

<body onload = "init()">

   <form>
    <input type="button" value="Start" onclick="moveRight()" />
    <br/><br/><br/><br><br/><br/><br/>
    <img id="red" src="redcar.png" alt="Car 1"/>
    <br/><br/><br/><br>
    <img id="blue" src="bluecar.png" alt ="Car 2" />

    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

不幸的是,有很多错误,很难知道从哪里开始。你的问题的第一个答案是按钮什么也没做,因为你的代码没有编译。我不知道为什么Dreamweaver没有报告错误。 Chrome的开发人员工具非常乐意这样做。

这是&#34;工作&#34;版本:

while ($row = mysql_fetch_array($res_final)) {
    if($row[5]== 's') {
        $row4_info = "";

        if (($row[4]== 'y')&& ($row[5]!= 's')) {
            $row4_info = "<input type='text' id='".{$row[0]}."' class='A{$count}' value=''>";
        }

        $row5_info = "";

        if ($row[5]== 's') {
            $row5_info = "<input type='text' id='".{$row[0]}."' class='A{$count}' value='' disabled='disabled'>";
        }

        echo "<tr> <td>". $sn = $startpoint + $count . "</td>
              <td class='C{$count}'>"  .$row[0] ."</td>
              <td >" . $row[1] . "</td>
              <td>" . $row[2] . "</td>
              <td>" . $row[3] . "</td>
              <td class='warning'>" . $row[4] . "</td>
              <td class='warning'>" . $row[5] . "</td>
              <td>" .$row4_info.$row5_info."</td></tr>";
    } 
    else {
        echo "<tr><td>" . $sn = $startpoint + $count . 
              "</td><td class='C{$count}'>"  .$row[0] ."</td>
              <td>" . $row[1] . "</td>
              <td>" . $row[2] . "</td>
              <td>" . $row[3] . "</td>
              <td>" . $row[4] . "</td>
              <td>" . $row[5] . "</td>
              <td>" . "<input type='text' id='{$row[0]}'
              class='A{$count}' value=''  />" . "</td></tr>";
    }

    $count = $count +1;
}