Este artículo describe el método para implementar el valor de color hexadecimal a RGB en JavaScript. Compártelo para su referencia. El método de implementación específico es el siguiente:
Copie el código de la siguiente manera: <! 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">
<Evista>
<meta http-equiv = "content-type" content = "text /html; charset = utf-8" />
<title> Valor de color hexadecimal a RGB </title>
<estilo>
*{margen: 0; Padding: 0; Font-Family: 'Microsoft Yahei'}
.Replace {ancho: 400px; altura: 210px; margen: 0 auto; almohadilla: 40px;}
.title {text-align: Center; Display: Block}
forma {ancho: 200px; margen: 30px auto;}
entrada {esquema: ninguno;}
entrada [type = "botón"] {cursor: pointer;}
</style>
<script>
función hextor (h) {
return parseInt ((Cuthex (H)). Subcadera (0, 2), 16)
}
función hextog (h) {
return parseInt ((Cuthex (H)). Subcandadora (2, 4), 16)
}
función hextob (h) {
return parseInt ((cuthex (h)). subcadena (4, 6), 16)
}
función cuthex (h) {
return h.charat (0) == "#"? H.Substring (1, 7): H
}
función setBGColorById (id, scolor) {
var elems;
if (document.getElementById) {
if (elems = document.getElementById (id)) {
if (elems.style) elems.style.backgroundcolor = scolor;
}
}
}
</script>
</ablo>
<Body>
<div>
<span> JavaScript Native Hexadecimal Color Value al valor RGB </span>
<form de nombre = "rgb">
<input value = "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); "valor =" 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>
El efecto de operación se muestra en la figura a continuación:
Espero que este artículo sea útil para la programación de JavaScript de todos.