2014-04-02 63 views
0

內的變量,如果我有使用如何訪問另一組變量

VAR箱=「RGBA(255,255,255,1)」中設置的變量;

如何以後只更改我選擇的顏色變成0-255?

例如,如果我使用的是滑塊以更改值...

<div class="slider"> 
<input type = "range" min="0" max="255" onchange="box=value"/> 
<output id="box">127</output> 
</div> 

如何只能引用只是一種顏色?

,然後在更先進的功能

var drawLine = function (x0, y0, x1, y1, alpha) { 
context.lineWidth = "hairline"; 
context.strokeStyle = "rgba(255,255,255," + alpha + ")"; 
context.beginPath(); 
context.moveTo(x0, y0); 
context.lineTo(x1, y1); 
context.closePath(); 
context.stroke(); 
}; 

例如說,我只希望的StrokeStyle只變成綠色一次。我怎樣才能在裏面引用這個變量?

回答

0

不要將字符串中的子字符串視爲「變量」;它只會讓你困惑。你問的是如何改變另一個字符串的子字符串。要做到這一點,您可以使用內置字符串的replacesplit方法(在後一種情況下,您可以使用join陣列方法重新組合該方法)。

e.g如果你想設置綠色零:

box=box.replace(/(\d+),(\d+),(\d+),(\d+)/, "$1,0,$3,$4");