最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript Change page background color with button - Stack Overflow

programmeradmin1浏览0评论

I am trying to figure out how to do code for changing the background color of a page with a button

so basically I have the code for a button written out and its function but im having trouble figuring out what code to use to make the button change the color of the entire page

<script>
    function changeColor()
    {

    };
    function changeText()
    {
        alert ("change text in this part");
    };
    function sendMsg()
    {
        alert ("change msg content here");
    }
</script>

<body>
    <h1>Wele to my program!</h1>
        <button onclick="changeColor()">Color</button>
        <button onclick="changeText()">Text</button> </br>
            <p>Can I ask you something Ive always wanted to ask the real Batman?</p> </br>
                 <input type="text" name="">
</body>

I am trying to figure out how to do code for changing the background color of a page with a button

so basically I have the code for a button written out and its function but im having trouble figuring out what code to use to make the button change the color of the entire page

<script>
    function changeColor()
    {

    };
    function changeText()
    {
        alert ("change text in this part");
    };
    function sendMsg()
    {
        alert ("change msg content here");
    }
</script>

<body>
    <h1>Wele to my program!</h1>
        <button onclick="changeColor()">Color</button>
        <button onclick="changeText()">Text</button> </br>
            <p>Can I ask you something Ive always wanted to ask the real Batman?</p> </br>
                 <input type="text" name="">
</body>
Share Improve this question asked Apr 5, 2013 at 1:34 Velija daviiiiVelija daviiii 151 gold badge2 silver badges5 bronze badges 2
  • You can get answer on internet via search engine: stackoverflow./questions/197748/… – Rong Nguyen Commented Apr 5, 2013 at 1:38
  • thank you so much ive been searching for 45 minutes and every search has brought me how to change the color of a button and not the background – Velija daviiii Commented Apr 5, 2013 at 1:47
Add a ment  | 

2 Answers 2

Reset to default 1
  document.body.style.background = color;

There's not much more to it, it's as simple as that!

Try out this code my dear :

<FORM> 
<INPUT type="button" value="Change to Yellow!" name="button3" onClick="document.bgColor='yellow'"> <br> 
<INPUT type="button" value="Change to Red!" name="button4" onClick="document.bgColor='red'"> <br> 
<INPUT type="button" value="Change back!" name="button5" onClick="document.bgColor='white'"> </FORM>
发布评论

评论列表(0)

  1. 暂无评论