JS 字体颜色怎么设置啊?这个怎么显示出的颜色?显示红色

JavaScript013

JS 字体颜色怎么设置啊?这个怎么显示出的颜色?显示红色,第1张

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"

}

js改变字体的颜色是用的“color”属性,xmlHttp.open("GET",URL,true)是设置ajax的请求地址和请求方式,不能去掉。

1、新建html文档,在body标签中添加p标签,标签内容是“演示文本”,这时字体的默认颜色是黑色的:

2、为了方便获取到这个p标签,给p标签添加上id,这里以“demo”为例:

3、添加script标签,在js标签中输入代码“document.getElementById('demo').style.color = '#f00'”,这样文本的字体颜色就变成了红色: