如何通过单击按钮更改网站的背景图像?

时间:2015-01-08 19:56:02

标签: javascript html css

我正在学习如何使用按钮和我的网站,我希望能够点击一个按钮,网站背景图像将会改变。我正在使用JavaScript和按钮标签。请帮我解决我的代码。

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="stylesheet.css" />
        <title> Test </title>
    </head>
    <body>
        <button id="changeBackground" onclick="changebgImage()">Change Background</button>
        <script>
function changebgImage() {
    document.body.style.background-image = url(Paradise.jpg);
}
        </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

document.body.style.backgroundImage= "url('img_tree.png')";

不是background-image

必须将字符串封装在引号中。

相关问题