JavaScript实现16进制颜色值转RGB的方法
作者:bea
本文实例讲述了JavaScript实现16进制颜色值转RGB的方法。分享给大家供大家参考。具体实现方法如下: 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
本文实例讲述了JavaScript实现16进制颜色值转RGB的方法。分享给大家供大家参考。具体实现方法如下:
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>16进制颜色值转RGB</title>
<style>
*{margin:0;padding:0;font-family:'Microsoft yahei'}
.replace{width:400px;height:210px;margin:0 auto;padding-top:40px;}
.title{text-align:center;display:block}
form{width:200px;margin:30px auto;}
input{outline:none;}
input[type="button"]{cursor:pointer;}
</style>
<script>
function hexToR(h) {
return parseInt((cutHex(h)).substring(0, 2), 16)
}
function hexToG(h) {
return parseInt((cutHex(h)).substring(2, 4), 16)
}
function hexToB(h) {
return parseInt((cutHex(h)).substring(4, 6), 16)
}
function cutHex(h) {
return h.charAt(0) == "#" ? h.substring(1, 7) : h
}
function setBgColorById(id, sColor) {
var elems;
if (document.getElementById) {
if (elems = document.getElementById(id)) {
if (elems.style) elems.style.backgroundColor = sColor;
}
}
}
</script>
</head>
<body>
<div class="replace">
<span class="title">JavaScript原生16进制颜色值转RGB值</span>
<form name="rgb">
<input value="ffffff" maxlength="7" size="16" name="hex" />
<input onclick="setBgColorById('colorSample',this.form.hex.value);
this.form.r.value=hexToR(this.form.hex.value);
this.form.g.value=hexToG(this.form.hex.value);
this.form.b.value=hexToB(this.form.hex.value);" value="转换" type="button" name="btn"/>
<br /><br />
R:<input style="width:30px" size="3" name="r" />
G:<input style="width:30px" size="3" name="g" />
B:<input style="width:30px" size="3" name="b" />
</form>
</div>
</body>
</html>
运行效果如下图所示:
希望本文所述对大家的javascript程序设计有所帮助。
有用 | 无用
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>16进制颜色值转RGB</title>
<style>
*{margin:0;padding:0;font-family:'Microsoft yahei'}
.replace{width:400px;height:210px;margin:0 auto;padding-top:40px;}
.title{text-align:center;display:block}
form{width:200px;margin:30px auto;}
input{outline:none;}
input[type="button"]{cursor:pointer;}
</style>
<script>
function hexToR(h) {
return parseInt((cutHex(h)).substring(0, 2), 16)
}
function hexToG(h) {
return parseInt((cutHex(h)).substring(2, 4), 16)
}
function hexToB(h) {
return parseInt((cutHex(h)).substring(4, 6), 16)
}
function cutHex(h) {
return h.charAt(0) == "#" ? h.substring(1, 7) : h
}
function setBgColorById(id, sColor) {
var elems;
if (document.getElementById) {
if (elems = document.getElementById(id)) {
if (elems.style) elems.style.backgroundColor = sColor;
}
}
}
</script>
</head>
<body>
<div class="replace">
<span class="title">JavaScript原生16进制颜色值转RGB值</span>
<form name="rgb">
<input value="ffffff" maxlength="7" size="16" name="hex" />
<input onclick="setBgColorById('colorSample',this.form.hex.value);
this.form.r.value=hexToR(this.form.hex.value);
this.form.g.value=hexToG(this.form.hex.value);
this.form.b.value=hexToB(this.form.hex.value);" value="转换" type="button" name="btn"/>
<br /><br />
R:<input style="width:30px" size="3" name="r" />
G:<input style="width:30px" size="3" name="g" />
B:<input style="width:30px" size="3" name="b" />
</form>
</div>
</body>
</html>
运行效果如下图所示:
希望本文所述对大家的javascript程序设计有所帮助。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- JavaScript设置body高度为浏览器高度的方法
- jquery动态改变div宽度和高度
- angularJS结合canvas画图例子
- jquery实现上下左右滑动的方法
- js实现上传图片预览的方法
- js实现ifram取父窗口URL地址的方法
- jquery实现相册一下滑动两次的方法
- js点击选择文本的方法
- JS动态加载当前时间的方法
- JavaScript实现Java中StringBuffer的方法
- angularJS 入门基础
- javascript中函数作为参数调用的方法
- javascript自动生成包含数字与字符的随机字符串
- angularJS 中$scope方法使用指南
- Javascript动态创建div的方法
- angularJS 中$attrs方法使用指南
- JavaScript实现获取dom中class的方法
- angularJS 中input示例分享
- angularJS提交表单(form)