为什么backgroundColor = rgb(a,b,c)不工作? [英] Why doesn't backgroundColor=rgb(a,b,c) work?
问题描述
< HTML>
< head>
< title>颜色< /标题>
< / head>
< body>
< script type =text / javascript>
var a = parseInt(prompt(Enter R));
var b = parseInt(prompt(Enter G));
var c = parseInt(prompt(Enter B));
document.body.style.backgroundColor = rgb(a,b,c);
< / script>
< / body>
< / html>
为什么背景颜色不会根据RGB值而改变?我做错了什么?
您需要使用引号:
document.body.style.backgroundColor ='rgb('+ a +','+ b +','+ c +')';
或者:
document.body.style.backgroundColor ='rgb('+ [a,b,c] .join(',')+')';
将一个, b
和 c
添加到一个未定义的函数 rgb ()
。当你设置CSS属性时,你需要传递一个字符串,因此需要引用 。 哦,你也是使用 parseInt()
它不需要需要传入一个基数,但是如果你这样做,它会更好(也更容易避免问题) 是预期的数字基数):
var a = parseInt(prompt(Enter R),10)|| 255,
b = parseInt(提示(输入G),10)|| 255,
c = parseInt(提示(输入B),10)|| 255;
JS Fiddle演示(在演示中,我使用 105
,所以很明显,如果 cancel 按钮被使用)。
如果有人在提示符上点击'取消',你可能想要提供一个默认参数来确保实际的颜色值被传递,因为 cancel ,否则,我认为,评估为 你当然也可以简单地定义一个函数: false
(我假设你更喜欢 255 $
$ b $函数rgb(r,g,b){
return'rgb('+ [(r || 0),(g || 0),(b || 0)]。join(',')+')';
var a = parseInt(prompt(Enter R),10),
b = parseInt(prompt(Enter G),10),
c = parseInt(提示(输入B),10);
document.body.style.backgroundColor = rgb(a,b,c);
这种方法具有允许使用自定义默认值的(可能似是而非的好处):
函数rgb(r,g,b,def){
def = parseInt(def,10)|| 0;
return'rgb('+ [(r || def),(g || def),(b || def)]。join(',')+')';
var a = parseInt(prompt(Enter R),10),
b = parseInt(prompt(Enter G),10),
c = parseInt(提示(输入B),10);
document.body.style.backgroundColor = rgb(a,b,c,40);
参考文献:
code> 。
parseInt()
。<html>
<head>
<title> Colors </title>
</head>
<body>
<script type="text/javascript">
var a = parseInt(prompt("Enter R"));
var b = parseInt(prompt("Enter G"));
var c = parseInt(prompt("Enter B"));
document.body.style.backgroundColor=rgb(a,b,c);
</script>
</body>
</html>
Why doesn't the background color change according to the RGB values? What have I done wrong??
You need to use quotes:
document.body.style.backgroundColor = 'rgb(' + a + ',' + b + ',' + c + ')';
Or:
document.body.style.backgroundColor = 'rgb(' + [a,b,c].join(',') + ')';
Unquoted the JavaScript is passing the variables, as arguments, a
,b
and c
to an undefined function called rgb()
. As you're setting a CSS property you need to pass a string, hence the requirement of quoting.
Oh, and also you're using parseInt()
which doesn't require a radix to be passed in, but it's better (and easier to avoid problems) if you do (the radix being the expected number-base):
var a = parseInt(prompt("Enter R"), 10) || 255,
b = parseInt(prompt("Enter G"), 10) || 255,
c = parseInt(prompt("Enter B"), 10) || 255;
JS Fiddle demo (In the demo I use 105
just so it's clear the default is being used if the cancel button is used).
And if someone hits 'cancel' on the prompt, you might want to supply a default argument to ensure that an actual colour-value is passed, since cancel otherwise, I think, evaluates to false
(I'm assuming you'd prefer 255
, but obviously adjust to taste).
You could also, of course, simply define a function:
function rgb(r,g,b) {
return 'rgb(' + [(r||0),(g||0),(b||0)].join(',') + ')';
}
var a = parseInt(prompt("Enter R"), 10),
b = parseInt(prompt("Enter G"), 10),
c = parseInt(prompt("Enter B"), 10);
document.body.style.backgroundColor = rgb(a,b,c);
And this approach has the (perhaps specious) benefit of allowing a custom default value to be used:
function rgb(r,g,b, def) {
def = parseInt(def, 10) || 0;
return 'rgb(' + [(r||def),(g||def),(b||def)].join(',') + ')';
}
var a = parseInt(prompt("Enter R"), 10),
b = parseInt(prompt("Enter G"), 10),
c = parseInt(prompt("Enter B"), 10);
document.body.style.backgroundColor = rgb(a,b,c,40);
References:
这篇关于为什么backgroundColor = rgb(a,b,c)不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!