简体   繁体   English

使用 javascript 变量更改 css 背景颜色

[英]changing css background color with javascript variables

I am trying to set the div 'changebox' with the color of the rgb which is the result of the three randoms.我正在尝试使用 rgb 的颜色设置 div 'changebox',这是三个随机的结果。 The rgbs are changing, (i can tell by the document.writes), but the div just stays white no matter what the rgb is. rgbs 正在改变,(我可以从 document.writes 看出),但无论 rgb 是什么,div 都保持白色。 Please help get the box to change to the correct color of the rgb.请帮助将框更改为 rgb 的正确颜色。

<div id="changeBox" style="width: 150px; height: 150px; position: absolute; top: 10%; left: 10%; background-color: white; border: 4px solid black; color: black; font-family: 'Merriweather Sans', sans-serif;">

<script>
var randR = Math.floor(Math.random() * 255) + 1;
document.write(randR+", ");
var randG = Math.floor(Math.random() * 255) + 1;
document.write(randG+", ");
var randB = Math.floor(Math.random() * 255) + 1;
document.write(randB);

document.getElementById('changeBox').style = "background-color: rgb(" + randR + ", " + randG + ", " + randB + ");";
</script>

</div>

Don't directly assign to style .不要直接分配给style Assign to style.backgroundColor .分配给style.backgroundColor

You should change only background-color property:您应该只更改background-color属性:

document.getElementById('changeBox').style.backgroundColor = "rgb(" + randR + ", " + randG + ", " + randB + ")";

In this case remove tailing ;在这种情况下,去除拖尾; to make value valid.使值有效。

Demo: http://jsfiddle.net/zc16vmjt/演示: http://jsfiddle.net/zc16vmjt/

Another thing is that document.write is not very good practice.另一件事是document.write不是很好的做法。 It's easy to avoid it in this case and go with appendChild :在这种情况下很容易避免它和 go 与appendChild

var box = document.getElementById('changeBox');
var randR = Math.floor(Math.random() * 255) + 1;
var randG = Math.floor(Math.random() * 255) + 1;
var randB = Math.floor(Math.random() * 255) + 1;
box.appendChild(document.createTextNode(randR + ", " + randG + ", " + randB));
box.style.backgroundColor = "rgb(" + randR + ", " + randG + ", " + randB + ")";

Demo: http://jsfiddle.net/zc16vmjt/1/演示: http://jsfiddle.net/zc16vmjt/1/

Each time when you:每次当您:

document.write();

You are overwriting what's in the body of the entire document, removing the div entirely.您正在覆盖整个文档正文中的内容,完全删除了 div。

var randR = Math.floor(Math.random() * 255) + 1;
var randG = Math.floor(Math.random() * 255) + 1;
var randB = Math.floor(Math.random() * 255) + 1;

document.getElementById('changeBox').style.backgroundColor = "rgb(" + randR+ "," + randG+ "," + randB + ")";

Try this and you should be fine.试试这个,你应该没事。 If you want to see the values, output to a separate div;如果要查看值,output 到单独的 div; Just don't overwrite the entire document with document.write();只是不要用 document.write(); 覆盖整个文档;

You have an extra semi-colon and are missing a closing quotation mark.您有一个额外的分号,并且缺少一个右引号。

To keep track of your nested quotes, it's a good idea to alternate between single and double quotes.要跟踪嵌套引号,最好在单引号和双引号之间交替使用。 Doing so makes it easier to realize that after the closing parenthesis, there should be a quote mark to close that and that should be followed by a quote that matches the beginning quote, before background-color.这样做可以更容易地意识到在右括号之后应该有一个引号来关闭它,并且应该在背景颜色之前跟一个与开始引号匹配的引号。

var theBox = document.getElementById('changeBox');
theBox.style = "background-color: rgb('+ randR+ ',' +randG+ ','+randB+')'";

In 2022 I am doing like this:在 2022 年,我正在这样做:

 let box = document.getElementsByClassName('color-box'); function changeColor(){ let random = Math.floor(Math.random()*9999999).toString(15); console.log(random) box[0].style.background = "#" + random; }
 .color-box{ height: 100px; width: 100px; border: 1px solid; }
 <div class="color-box" onclick="changeColor()"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM