开发者

JavaScript change color problem

I've seen all the posts but my problem isn't that it doesn't change the background color but rather that it does and the change it but to the original. Here's the code.

    <html>
        <head>
            <title>Calculator</title>
            <link rel="stylesheet" type="text/css" href="color.css"/>
            <script type="text/javascript">
                function calc(color){
                    //document.body.bgColor = color;
                   x=document.myForm
                   var val1=x.input1.value;
                   var val2=x.input2.value;
                   var val3=val1+"+"+val2;
                   if(eval(val3)<0) return;
                    else{

                   alert("The result is "+eval(val3));
                    var bd = document.getElementById ('body');
                    bd.className="highlight";
                    return;
                    }
                }
            </script>
        </head>
        <body id="body" onload="changeBackground(red)">
            <form name="myForm" onsubmit="calc(this)">
                Enter 2 values to add ;)
                <input type="text" name="input1">
                <开发者_运维知识库;input type="text" name="input2">
                <input type="submit" value="Send Input">
            </form>
        </body>
    </html>

CSS:

    .hightlight{
       background-color: yellow;
    }

I understand that the function ends right before the end of the function. So how can I get it to hold the color?


The problem is that you tries to POST page over submit button. In this case page is fully redrawn, without keeping previous state that is set by your script. Just change code of button:

<input type="button" value="Send Input" onclick="calc(this)">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜