Artikel ini menjelaskan metode menerapkan nilai warna heksadesimal ke RGB di JavaScript. Bagikan untuk referensi Anda. Metode implementasi spesifik adalah sebagai berikut:
Salin kode sebagai berikut: <! Doctype html public "-// w3c // dtd xhtml 1.0 transisi // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv = "konten tipe" content = "text /html; charset = utf-8" />
<title> nilai warna hexadecimal ke rgb </iteme>
<tyle>
*{margin: 0; padding: 0; font-family: 'microsoft Yahei'}
.replace {lebar: 400px; tinggi: 210px; margin: 0 auto; padding-top: 40px;}
.title {text-align: center; display: block}
Formulir {lebar: 200px; margin: 30px auto;}
input {outline: none;}
input [type = "tombol"] {kursor: pointer;}
</tyle>
<script>
fungsi hextor (h) {
return parseint ((cuthex (h)). substring (0, 2), 16)
}
fungsi hextog (h) {
return parseint ((cuthex (h)). substring (2, 4), 16)
}
fungsi hextob (h) {
return parseint ((cuthex (h)). substring (4, 6), 16)
}
fungsi cuthex (h) {
return h.charat (0) == "#"? H.Substring (1, 7): h
}
fungsi setBgColorById (id, scolor) {
var elem;
if (document.geteLementById) {
if (elems = document.geteLementById (id)) {
if (elems.style) elems.style.backgroundcolor = scolor;
}
}
}
</script>
</head>
<body>
<div>
<span> JavaScript Native Hexadecimal Warna Nilai untuk Nilai RGB </span>
<Form name = "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 =" tombol "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>
Efek operasi ditunjukkan pada gambar di bawah ini:
Saya harap artikel ini akan membantu pemrograman JavaScript semua orang.