Este artigo descreve o método de implementação do valor de cor hexadecimal para RGB no JavaScript. Compartilhe para sua referência. O método de implementação específico é o seguinte:
Copie o código da seguinte forma: <! Doctype html public "-// w3c // dtd xhtml 1.0 transitória // pt" "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> Valor de cor hexadecimal para RGB </title>
<estilo>
*{margem: 0; preenchimento: 0; font-família: 'Microsoft yahei'}
.Place {Width: 400px; Hight: 210px; margem: 0 Auto; Top-top: 40px;}
.Title {Text-Align: Center; Display: Block}
Formulário {Width: 200px; margem: 30px Auto;}
entrada {esboço: nenhum;}
input [type = "button"] {cursor: pontenter;}
</style>
<Cript>
função hextor (h) {
Retornar parseint ((cuthex (h)). Substring (0, 2), 16)
}
função hextog (h) {
Retornar Parseint ((Cuthex (H)). Substring (2, 4), 16)
}
função hextob (h) {
Retornar Parseint ((Cuthex (H)). Substring (4, 6), 16)
}
função cuthex (h) {
Retornar H.Charat (0) == "#"? H.Substring (1, 7): H
}
função setbgcolorbyId (id, scolor) {
var elems;
if (document.getElementById) {
if (elems = document.getElementById (id)) {
if (elems.style) elems.style.backgroundColor = scolor;
}
}
}
</script>
</head>
<Body>
<div>
<pan> javascript nativo hexadecimal color valor para rgb valor </span>
<name de forma = "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); "value =" convert "type =" button "name =" btn "/>
<r /> <r />
R: <size de entrada = "3" name = "r" />
G: <size de entrada = "3" name = "g" />
B: <size de entrada = "3" name = "b" />
</morm>
</div>
</body>
</html>
O efeito de operação é mostrado na figura abaixo:
Espero que este artigo seja útil para a programação JavaScript de todos.