บทความนี้อธิบายถึงวิธีการใช้ค่าสีเลขฐานสิบหกกับ RGB ใน JavaScript แบ่งปันสำหรับการอ้างอิงของคุณ วิธีการใช้งานเฉพาะมีดังนี้:
คัดลอกรหัสดังนี้: <! 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">
<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-allign: center; display: block}
แบบฟอร์ม {width: 200px; margin: 30px auto;}
อินพุต {โครงร่าง: ไม่มี;}
อินพุต [type = "ปุ่ม"] {เคอร์เซอร์: ตัวชี้;}
</style>
<script>
ฟังก์ชั่น Hextor (h) {
ส่งคืน parseint ((cuthex (h)) substring (0, 2), 16)
-
ฟังก์ชั่น hextog (h) {
ส่งคืน parseint ((cuthex (h)) substring (2, 4), 16)
-
ฟังก์ชั่น hextob (h) {
ส่งคืน parseint ((cuthex (h)) substring (4, 6), 16)
-
ฟังก์ชั่น cuthex (h) {
ส่งคืน h.charat (0) == "#"? H.Substring (1, 7): H
-
ฟังก์ชั่น 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 hexadecimal สีค่า RGB ค่า </span>
<form name = "rgb">
<ค่าอินพุต = "ffffffff" maxLength = "7" size = "16" name = "hex" />
<อินพุต onClick = "setBgColorById ('coloursample', 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: <อินพุตขนาด = "3" name = "r" />
g: <อินพุตขนาด = "3" name = "g" />
B: <อินพุตขนาด = "3" name = "b" />
</form>
</div>
</body>
</html>
เอฟเฟกต์การดำเนินการแสดงในรูปด้านล่าง:
ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม JavaScript ของทุกคน