如何使用JS功能更改屏幕BGcolor?

时间:2022-11-12 11:20:23

I'm trying to create a simple button to change the background color using JS function. can't get it to work though...

我正在尝试使用JS函数创建一个简单的按钮来更改背景颜色。虽然不能让它工作......

<!doctype html><html>
<head>
<title>Blank Page</title>
    <script type="text/javascript">

        var bg = function(){
            var color = prompt("Please enter a color" , "Color");
            return color;
        }

    </script>
</head>
<body>
<input type="button" value="Change BG" onClick="document.bgColor='bg()'">

</body>
</html>`

4 个解决方案

#1


1  

You need to remove the '' from 'bg()' Like so.

你需要删除''来自'bg()'就像这样。

Please find the document.bgColor is deprecated since HTML4.0, so it's ill advised using it.

请查找document.bgColor自HTML4.0以来已弃用,因此不建议使用它。

<script>
    function bg(){
    var color = window.prompt("Please pick color");
    return color;
    }
    </script>
    </head>
    <body>
    <input type="button" value="changeBG" onclick="document.bgColor=bg();"/>

    </body>
    </head>

#2


0  

Call function without single quotes and The color entered in color codes should start with '#'

不带单引号的调用函数和颜色代码中输入的颜色应以'#'开头

var color = prompt("Enter the color you want on the Background???");
document.body.style.backgroundColor = color;

It'll work for you

它会对你有用

#3


0  

Like this:

http://jsfiddle.net/50zj6yxj/

<input type="button" value="Change BG" onclick="change()">

var change = function () {
    var color = prompt("Please enter color", "#000000");
    if (color != null) {
        document.body.style.background = color;
    }
}

#4


0  

Wrap your code when dom is ready:

当dom准备好时包裹你的代码:

window.onload=function() {
    var bg = function(){
        var color = prompt("Please enter a color" , "Color");
        return color;
    }
}

#1


1  

You need to remove the '' from 'bg()' Like so.

你需要删除''来自'bg()'就像这样。

Please find the document.bgColor is deprecated since HTML4.0, so it's ill advised using it.

请查找document.bgColor自HTML4.0以来已弃用,因此不建议使用它。

<script>
    function bg(){
    var color = window.prompt("Please pick color");
    return color;
    }
    </script>
    </head>
    <body>
    <input type="button" value="changeBG" onclick="document.bgColor=bg();"/>

    </body>
    </head>

#2


0  

Call function without single quotes and The color entered in color codes should start with '#'

不带单引号的调用函数和颜色代码中输入的颜色应以'#'开头

var color = prompt("Enter the color you want on the Background???");
document.body.style.backgroundColor = color;

It'll work for you

它会对你有用

#3


0  

Like this:

http://jsfiddle.net/50zj6yxj/

<input type="button" value="Change BG" onclick="change()">

var change = function () {
    var color = prompt("Please enter color", "#000000");
    if (color != null) {
        document.body.style.background = color;
    }
}

#4


0  

Wrap your code when dom is ready:

当dom准备好时包裹你的代码:

window.onload=function() {
    var bg = function(){
        var color = prompt("Please enter a color" , "Color");
        return color;
    }
}