[英]How do I change website background
I've made a quiz which has 10 questions, and stores your points in a value called total
. 我做了一个有10个问题的测验,并将你的积分存储在一个名为
total
的值中。 The total points you are able to get is 20, so when total > 10
, I want the background to turn red . 你可以得到的总分是20,所以当
total > 10
,我希望背景变成红色 。
I have already set a background using CSS on my website here: 我已经在我的网站上使用CSS设置了一个背景:
<style type="text/css">
body{
background-image: url("twins.jpg");
}
However I can't seem to get my condition to work properly. 但是我似乎无法使我的状况正常工作。 I've tried:
我试过了:
if (total > 10) {
document.body.style.backgroundColor = "red";
}
I've tried: 我试过了:
if (total > 10) {
document.body.style.backgroundColor = "#AA0000";
}
And: 和:
if (total > 10) {
document.getElementById('body').style.backgroundImage = "url(ashishot.jpg)";
}
But nothing seems to work. 但似乎没有任何效果。 Maybe I'm placing my
if
statement in the wrong place, or maybe I'm trying to set the background incorrectly, I just want to know if this is the correct way to change a background image from CSS into JavaScript. 也许我把
if
语句放在错误的地方,或者我正在尝试错误地设置背景,我只是想知道这是否是将背景图像从CSS更改为JavaScript的正确方法。
Yes this is the correct way to change background color using JS. 是的,这是使用JS更改背景颜色的正确方法。
document.body.style.backgroundColor = "#AA0000";
Please try: 请试试:
alert()
inside the IF block to see if block is executing alert()
置于IF块内以查看块是否正在执行 The javascript instruction is correct javascript指令是正确的
document.body.style.backgroundColor = "red";
Your error may be the if condition, check if total its work properly usign the console or an alert to check it: 您的错误可能是if条件,请检查其工作是否正确使用控制台或警报进行检查:
console.log(total);
or 要么
alert(total);
You have more than one issue 你有不止一个问题
Use directly the body
property 直接使用
body
属性
document.body.style.backgroundImage
You need to quote the whole assignment and you need inner quotes for the image 您需要引用整个作业,并且需要图像的内部引号
'url("ashishot.jpg")';
Working example: 工作范例:
function setBackground() { document.body.style.backgroundImage = 'url("http://lorempixel.com/300/200/")'; } setTimeout(setBackground, 2000);
body { background-image: url("http://lorempixel.com/400/200/"); }
if (total > 10) {
document.body.style.backgroundImage = "url(ashishot.jpg)";
}
body
is not an id. body
不是身份证。 also, setting background-color
will not work because browser treat background-image
first. 此外,设置
background-color
将无法工作,因为浏览器首先处理background-image
。 To display background-color
instead of background-image
, simply delete the image. 要显示
background-color
而不是background-image
,只需删除图像即可。 Setting classes is more flexible. 设置类更灵活。
document.body.classList.add("total");
body { background-image: url("http://www.studiocity-macau.com/uploads/images/SC/Entertainment/Batman/batman_share.jpg"); } body.total { background-image: none; background-color: red; }
Be aware that your backgound-image
will be on top of the color you are setting in the if
block. 请注意,您的
backgound-image
将位于您在if
块中设置的颜色之上。 If the image covers the background entirely you will not notice the effect of changing the background color
. 如果图像完全覆盖背景,您将不会注意到更改
background color
的效果。
Both document.body.style.backgroundColor = "#AA0000"
and document.body.style.backgroundColor = "red"
are valid (although "red" == "#FF0000"
). document.body.style.backgroundColor = "#AA0000"
和document.body.style.backgroundColor = "red"
都有效(尽管"red" == "#FF0000"
)。
Make sure the if
statement runs every time total
increases. 确保
if
语句在每次total
增加时运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.