تصف هذه المقالة طريقة تنفيذ قيمة الألوان السداسية إلى RGB في JavaScript. شاركه للرجوع إليه. طريقة التنفيذ المحددة هي كما يلي:
انسخ الرمز على النحو التالي: <! doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv = "content-type" content = "text /html ؛ charset = utf-8" />
<title> قيمة اللون السداسي عشر إلى RGB </title>
<style>
*{margin: 0 ؛ padding: 0 ؛ font-family: 'Microsoft Yahei'}
.replace {Width: 400px ؛ الارتفاع: 210px ؛ الهامش: 0 Auto ؛ Padding-Top: 40px ؛}
.title {text-align: center ؛ display: block}
النموذج {العرض: 200px ؛ الهامش: 30px Auto ؛}
إدخال {الخطوط العريضة: لا شيء ؛}
إدخال [type = "button"] {cursor: pointer ؛}
</style>
<script>
وظيفة hextor (H) {
إرجاع Parseint ((Cuthex (H)). Sundring (0 ، 2) ، 16)
}
وظيفة hextog (H) {
إرجاع Parseint ((Cuthex (H)). Sundring (2 ، 4) ، 16)
}
وظيفة hextob (H) {
إرجاع Parseint ((Cuthex (H)). Sundring (4 ، 6) ، 16)
}
وظيفة cuthex (h) {
إرجاع H.charat (0) == "#"؟ H.SubString (1 ، 7): ح
}
وظيفة setBgColorById (معرف ، scolor) {
var elems ؛
if (document.getElementById) {
if (elems = document.getElementById (id)) {
إذا (elems.style) elems.style.backgroundColor = scolor ؛
}
}
}
</script>
</head>
<body>
<viv>
<span> javaScript Native Quexadecimal Value to RGB Value </span>
<name form = "rgb">
<value input = "ffffffff" 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 =" convert "type =" button "name =" btn "/>
<br /> <br />
R: <input size = "3" name = "r" />
G: <input size = "3" name = "g" />
B: <input size = "3" name = "b" />
</form>
</div>
</body>
</html>
يظهر تأثير التشغيل في الشكل أدناه:
آمل أن تكون هذه المقالة مفيدة لبرمجة JavaScript للجميع.