Dieser Artikel beschreibt die Methode zur Implementierung des hexadezimalen Farbwerts in RGB in JavaScript. Teilen Sie es für Ihre Referenz. Die spezifische Implementierungsmethode lautet wie folgt:
Kopieren Sie den Code wie folgt: <! 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">
<kopf>
<meta http-äquiv = "content-type" content = "text /html; charset = utf-8" />
<titels> hexadezimaler Farbwert für RGB </title>
<Styles>
*{Margin: 0; Polster: 0; Schriftfamilie: 'Microsoft Yahei'}
.Replace {Breite: 400px; Höhe: 210px; Rand: 0 Auto; Padding-Top: 40px;}
.title {text-align: center; Anzeige: Block}
Form {width: 200px; Margin: 30px Auto;}
Eingabe {Umriss: Keine;}
input [type = "button"] {cursor: pointer;}
</style>
<Script>
Funktion hextor (h) {
Return ParseInt ((Cuthex (h)). Substring (0, 2), 16)
}
Funktion hextog (h) {
Return ParseInt ((Cuthex (h)). Substring (2, 4), 16)
}
Funktion hextob (h) {
Return ParseInt ((Cuthex (H)). Substring (4, 6), 16)
}
Funktion cuthex (h) {
H.CHARAT (0) == "#" zurückgeben? H.Substring (1, 7): H.
}
Funktion setBgColorbyId (id, scolor) {
var elems;
if (document.getElementById) {
if (elems = document.getElementById (id)) {
if (Elems.Style) Elems.Style.BackgroundColor = Scolor;
}
}
}
</script>
</head>
<body>
<div>
<span> JavaScript native hexadezimale Farbwert zum RGB -Wert </span>
<form name = "rgb">
<input value = "ffffffffff" 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 =" konvertieren "type =" button "name =" btn "/>
<br /> <br />
R: <Eingabegröße = "3" name = "r" />
G: <Eingabegröße = "3" name = "g" />
B: <Eingabegröße = "3" name = "b" />
</form>
</div>
</body>
</html>
Der Betriebseffekt ist in der folgenden Abbildung dargestellt:
Ich hoffe, dieser Artikel wird für JavaScript -Programme aller hilfreich sein.