ใช้เมธอด fontcolor() ของ String กำหนดสีฟอนต์ เหมือนแท็ก <font color="..."> (deprecated)
ตัวอย่าง
<html>
<head>
<title>JavaScript String fontcolor() Method</title>
</head>
<body>
<script>
var str = new String("Demo Text");
document.write(str.fontcolor("blue"));
alert(str.fontcolor("blue"));
</script>
</body>
</html>
ผลลัพธ์
<font color="blue">Demo Text</font>
⚠️ Legacy method – ใช้ CSS แทน:
// CSS
.element { color: blue; }
.element { color: #0000ff; }
.element { color: rgb(0, 0, 255); }
.element { color: hsl(240, 100%, 50%); }
// JavaScript
element.style.color = "blue";
Color formats
| Format | ตัวอย่าง |
|---|---|
| Named color | red, blue, green, transparent |
| Hex | #ff0000, #f00 |
| RGB | rgb(255,0,0), rgba(255,0,0,0.5) |
| HSL | hsl(0, 100%, 50%), hsla(0, 100%, 50%, 0.5) |