//right window "bakgrundsfärg
function readingFrontValues_right(){
//getting the values from the textboxes
var text1 = parseInt(document.getElementById("backred").value);
var hex1 = text1.toString(16);
var text2 = parseInt(document.getElementById("backgreen").value);
var hex2 = text2.toString(16);
var text3 = parseInt(document.getElementById("backblue").value);
var hex3 = text3.toString(16);
//presenting the result
var checkingHexa_2 = "#";
checkingHexa_2 += hex1;
checkingHexa_2 += hex2;
checkingHexa_2 += hex3;
var setText_2 = document.getElementById(("backgroundcolorhex"));
//setting the hexvalue in the last-textbox
setText_2.value = checkingHexa_2;
//var backColor = document.getElementById("colorslab");
// backColor.style.backgroundColor = checkingHexa_2;
setBgColorById("colorslab", checkingHexa_2);
alert(checkingHexa_2);
// var bgcolor = document.getElementById("colorslab");
// bgcolor.style.backgroundColor = checkingHexa_2;
// addingResult(checkingHexa);
}
function setBgColorById(id,sColor) {
var elem;
if (document.getElementById) {
if (elem = document.getElementById(id)) {
if (elem.style) {
elem.style.backgroundColor=sColor;
return 1; // success
}
}
}
return 0; // failure
}
答案 0 :(得分:0)
您的代码将返回4和5个字符的十六进制代码,这些代码无法用作HTML颜色代码。
为什么不使用这样的东西? (snaffled from here)
function rgbToHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
function toHex(n) {
n = parseInt(n,10);
if (isNaN(n)) return "00";
n = Math.max(0,Math.min(n,255));
return "0123456789ABCDEF".charAt((n-n%16)/16)
+ "0123456789ABCDEF".charAt(n%16);
}
答案 1 :(得分:0)
应在rgb十进制上应用parseInt时指定10作为基数, 并且必须将任何一个字符十六进制值填充到2个位置。
A将变为'0A',0将为'00',9将变为'09',依此类推。