标签:
HTML:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>RGB颜色转换</title>
<script language="javascript">
function showRGB(obj) {
// 其中red、green、blue都是输入框对应的name值
var oRed = obj.red.value;
var oGreen = obj.green.value
var oBlue = obj.blue.value;
//将RGB转换为16进制Hex值,字符串
oHexcode = "#" + toHex(oRed) + toHex(oGreen) + toHex(oBlue);
var oDiv = document.getElementById("div");
// div的背景色=name为hexval输入框的值=16进制背景色的值
oDiv.style.background = obj.hexval.value = oHexcode;
}
function toHex(d) {
// 如果输入的不是数字
if (isNaN(d)) {
d = 0;
}
// 16进制转换方法
var n = new Number(d).toString(16);
return (n.length == 1 ? "0" + n : n);
}
</script>
</head>
<body>
<form style="border: 1px blue solid;">
<b>请输入RGB颜色值(0 to 255)</b><br>
Red: <input type="text" name="red" size="5"><br>
Green: <input type="text" name="green" size="5"><br>
Blue: <input type="text" name="blue" size="5"><br>
<button onClick="showRGB(this.parentNode)" type="button">显示Hex #</button>
Hex值为: <input type="text" name="hexval" size="7">
</form>
<div id="div" style="margin: 50px;width: 200px;height: 200px;border: 1px solid red;"></div>
</body>
</html>
result:
标签:
原文地址:http://www.cnblogs.com/theWayToAce/p/5493207.html