0

我想做类似的东西:

1. 1296
2. 4624

Square root both numbers and add the result! 
Answer: ________
[Check button]

并打开somewhere.html如果答案正确

我制作了这个脚本:

<html>
<head>
    <script language="javascript">
        window.onload=calc; 

        function calc() {
            var num1, num2, sqNum1, sqNum2, randomNum, sum, ans;
            num1=Math.floor(Math.random()*90)+10;
            num2=Math.floor(Math.random()*90)+10;
            sqNum1=num1*num1;
            sqNum2=num2*num2;
            sum=num1+num2;
            document.write("1. " + sqNum1 + "<br />");
            document.write("2. " + sqNum2 + "<br />");
            document.write("<br />");
        }

        function checkAns(form) {
            if (form.ans.value==sum) {
                location="somewhere.html";
            } else {
                alert ("Wrong answer!");
            }
        }
    </script>
</head>

<body>
    Square root both numbers and add the result! <br />
    <form name='question'>
        Answer: <input name='ans' type='password'> <br />
        <input type='button' value='Check' onClick='checkAns(this.form)'>
    </form>
</body>
</html>

无论我尝试什么,它都不会显示这部分:

Square root both numbers and add the result! 
Answer: ________
[Check button]

这个脚本有什么问题?

4

2 回答 2

5

load函数被调用时,文档就处于关闭状态。

您不能write将文档置于关闭状态,因此write会呼叫open您。

调用open将清除现有文件。

新内容被写入新文档。

使用DOM 操作createElementappendChild朋友)编辑现有文档。

于 2013-09-23T14:02:47.380 回答
-1

你需要这样的东西:

<html>
<head>
    <script language="javascript">
        function calc() {
            var num1, num2, sqNum1, sqNum2, randomNum, sum, ans;
            num1=Math.floor(Math.random()*90)+10;
            num2=Math.floor(Math.random()*90)+10;
            sqNum1=num1*num1;
            sqNum2=num2*num2;
            sum=num1+num2;
            document.getElementById("sum").value = sum;

            var question = document.getElementById("question");
            var numbers = document.getElementById("numbers");
            var br = document.createElement('br');
            var txt = document.createTextNode("1. " + sqNum1);
            numbers.appendChild(txt);
            numbers.appendChild(br);
            var txt = document.createTextNode("2. " + sqNum2);
            numbers.appendChild(txt);
            txt.appendChild(question);
        }

        function checkAns(form) {

            if (document.getElementById("ans").value == document.getElementById("sum").value) {
                var newWin = open('somewhere.html','windowName','height=300,width=300');
            } else {
                alert ("Wrong answer!");
            }
        }
    </script>
</head>

<body onload="calc();">
    <form name='question'>
        <div id="numbers">
        </div>
        <br/>
        Square root both numbers and add the result! <br />
        <div id="question">
            Answer: <input id='ans' name='ans' type='password'> <br />
            <input type='button' value='Check' onClick='checkAns(this.form)'>
        </div>
        <input id='sum' type='hidden' value=''/>
    </form>
</body>
</html>

这不是最好的答案,它是您可以用来跟随您的开发的一种方式

于 2013-09-23T14:22:31.217 回答