在HTML和JavaScript中打印1到5甚至奇数

时间:2017-07-05 11:59:41

标签: javascript html

我必须创建一个满足以下要求的html页面 - 1.用另一个阵列打印1到5 2.从文本框中输入数字,然后在页面上打印,无论是偶数还是奇数。

我创建了一个页面,但它没有按预期工作。

    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
        <p id="demo"></p>
        <input type="number" id="myNumber">
        <button onclick="oddOrEven()">Try it</button>
        <input type="text" name="result" id="result" readonly="true"/>
            <SCRIPT>    
            var numbers = [1,2,3,4,5];
            var myObjects = ["One","Two","Three","Four","Five"];
            var text = "";
            var myObjectsList =""
            var i;
            for (i = 0; i < numbers.length; i++) {
                text += numbers[i] + " : " + myObjects[i] + "<br>";
            }
            document.getElementById("demo").innerHTML = text;

            function oddOrEven() {
            var value = document.getElementById("myNumber").value;
            var res = if((value % 2) == 0) {"Even"} else {"Odd"}*/
            //if(value % 2 == 0) document.write("Even")
            //document.write(value);
            //document.getElementById("demo").innerHTML = res;
            readonly.value=res;
            }
            //document.write("Hello World!");

        </SCRIPT>

    </BODY>
    </HTML>

有人可以帮我解释一下代码。

2 个答案:

答案 0 :(得分:0)

您分配res的方式存在问题,应该如下:

var res = value % 2 == 0 ? "Even" : "Odd";

如果您愿意,也可以使用if语句:

var res = "Odd";
if(value % 2 == 0) {
    res = "Even";
}

此外,这一行还存在一个问题:

readonly.value=res;

在这种情况下,readonly不存在。你可能意味着这样做:

document.getElementById("result").value = res;

答案 1 :(得分:0)

请参阅更新的解决方案

&#13;
&#13;
            
            var numbers = [1,2,3,4,5];
            var myObjects = ["One","Two","Three","Four","Five"];
            var text = "";
            var myObjectsList =""
            var i;
            for (i = 0; i < numbers.length; i++) {
                text += numbers[i] + " : " + myObjects[i] + "<br>";
            }
            document.getElementById("demo").innerHTML = text;

            function oddOrEven() {
            var value = document.getElementById("myNumber").value;
            var res = value % 2 == 0 ? "Even" : "Odd";
            document.getElementById("result").value=res;
            }
            //document.write("Hello World!");
&#13;
<p id="demo"></p>
        <input type="number" id="myNumber">
        <button onclick="oddOrEven()">Try it</button>
        <input type="text" name="result" id="result" readonly/>
&#13;
&#13;
&#13;

更新了代码

&#13;
&#13;
var numbers = [1,2,3,4,5];
var myObjects = ["One","Two","Three","Four","Five"];
var text = "";
var myObjectsList =""
var i;
for (i = 0; i < numbers.length; i++) {
    text += numbers[i] + " : " + myObjects[i] + "<br>";
}
document.getElementById("demo").innerHTML = text;

function oddOrEven() {
var value = document.getElementById("myNumber").value;
var res = value % 2 == 0 ? "Even" : "Odd";
document.getElementById("result").innerHTML=res;
}
&#13;
<p id="demo"></p>
<input type="number" id="myNumber">
<button onclick="oddOrEven()">Try it</button>
<p id="result"></p>
&#13;
&#13;
&#13;

UPDATE2 取代

<p id="result"></p>

<span id="result"></span>