当点击submit后,使用.getElementById获取id number并使用.value获得他的值储存在number中

可使用i%2==0与i%2==1获得偶数和奇数

得到奇数和b与偶数和后a

使用.getElementById获取id id jishu与id oushu的内容用.innerHTML改变为b和a

javascript 奇数偶数之和 js求奇数和_javascript

 

javascript 奇数偶数之和 js求奇数和_html_02

 

 

javascript 奇数偶数之和 js求奇数和_i++_03

 

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
    window.onload = function(){
        var subBtn=document.getElementById("addEmpButton");
        subBtn.onclick=function(){
            var number=document.getElementById("number").value;
            var a=0;
            var b=0;
            for(var i=1;i<=number;i++){
                if(i%2==0){
                a+=i;
            }
            document.getElementById("oushu").innerHTML=a
            }
            for(var i=1;i<=number;i++){
                if(i%2==1){
                b+=i;
            }
            document.getElementById("jishu").innerHTML=b
            }
            document.getElementById("jishuz").innerHTML=number+"以内的奇数和是:"
            document.getElementById("oushuz").innerHTML=number+"以内的偶数和是:"
        }

    }
    </script>
    请输入一个数<input type="text" name="number" id="number" /><br>
    <button id="addEmpButton" value="abc">
        Submit
    </button><br>
    <span id="jishuz"></span><span id="jishu"></span><br>
    <span id="oushuz"></span><span id="oushu">将得到该数内所有奇数的和与偶数的和</span>
</body>
</html>