html值不应刷新为零

时间:2014-08-13 02:56:06

标签: html

如果两张图片相同,则应加1,否则扣除1.问题是变量点刷新为0。 我的代码

<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!-- Fig. 10.4 RandInt.html          -->
<!-- Demonstrating the Random Method -->

<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
    <title>Picture Random</title>

</head>
<body>
    <form action = "PictRand.html">
        <p>Click Generate to generate another set of images.</p> 
        <input type = "submit" value = "Generate">
    </form>


</body>
<script type = "text/javascript">
            <!--
            var s1, s2, point = 0, score, x;

            s1 = Math.floor ( 1 + Math.random() * 3 );
            s2 = Math.floor ( 1 + Math.random() * 3 );

        //  score = point;
    document.write ( "<img src = \"" + s1 + ".jpg\" width = \"200\" height = \"200\" />");
    document.write ( "<img src = \"" + s2 + ".jpg\" width = \"200\" height = \"200\" />");


            if(s1 != s2)
            {
                            point=point-1;
            document.write ( "Failed!   Score: " + point);

            }
            else 
            if( s1 == s2 ) 
                {
                    point = point + 1;
                    document.write("Score: " + point);

                }
            //-->   
        </script>
              </html>

如果两张图片相同,则应加1,否则扣除1.问题是变量点刷新为0.我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

尝试类似:

<body>
  <input type="button" value="Generate" onclick="generateImages();">
</body>
<script type = "text/javascript">
<!--
 var point = 0;
 function generateImages() {
   var s1, s2, x;

   s1 = Math.floor(1 + Math.random() * 3);
   s2 = Math.floor(1 + Math.random() * 3);
   document.write("<img src = \"" + s1 + ".jpg\" width = \"200\" height = \"200\" />");
   document.write("<img src = \"" + s2 + ".jpg\" width = \"200\" height = \"200\" />");

   if (s1 != s2) {
     point--;
     document.write ( "Failed!   Score: " + point);
   } else {
     point++;
     document.write("Score: " + point);
   }
 }
 generateImages();
//-->   
</script>