开发者

rgb value as variable

How do I store a variable value in a rgb() ? 开发者_如何学JAVAI use this code which isn't working:

<script>
var R=200;
var colval="rgb(R,10,100)";
</script>

I want it to be like this:

<script>
var colval="rgb(200,10,100)";
</script>

but somehow it doesn't store R right , putting quotes around 200 or R isn't working either.


I assume you're using JavaScript:

<script>
    var R = 200;
    var colval = "rgb(" + R + ",10,100)";
</script>

Results in colval = rgb(200,10,100)


Use the "+" because in javascript the is the operator used to concatenate, or add things together to make a string. In this case we are adding together "rgb("+ r +","+ g +","+ b +")" to make a string that looks like "rgb(225, 34, 154)". When the browser's JS interpreter reads this code, "rgb("+ r +","+ g +","+ b +")", it will replace the r, g, and b with the values of those variables.


Now you could use template string:

    <script>
        var R = 200;
        var colval = `rgb(${R}, 10, 100)`;
    </script>

    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜