色,十六进制色和名字组成的数组或者字典,然后查询颜色会比较好。
<html><script type="text/javascript">
function f(n)
{
switch(n)
{
case 0:
alert("红")
break
case 1:
alert("黄")
break
case 2:
alert("粉")
break
case 3:
alert("绿")
break
case 4:
alert("蓝")
break
}
}
</script>
<form>
<input type="button" value="红" onclick="f(0)" />
<input type="button" value="黄" onclick="f(1)" />
<input type="button" value="粉" onclick="f(2)" />
<input type="button" value="绿" onclick="f(3)" />
<input type="button" value="蓝" onclick="f(4)" />
</form>
</html>
JS设置字体颜色是通过修改css样式来实现的。html页面:
<body>
<form name="form1">
<input type="text" name="title" id="title" value="黑色头发" style="font-family:Helvetica">
<select name="selector" id="selector" onchange="changeColor()">
<option value="one">红色 </option>
<option value="two">绿色 </option>
</select>
<select name="selector2" id="selector2" onchange="changeStyle()">
<option value="one">宋体 </option>
<option value="two">楷体 </option>
</select>
</form>
</body>
</html>
js改变字体颜色的方法:
<script>
function changeColor()
{
var title = document.getElementById("title").value
var select = document.getElementById("selector").value
if(select=="two")
{
document.getElementById("title").style.color="green"
}
else document.getElementById("title").style.color="red"
}