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'”,这样文本的字体颜色就变成了红色:
<!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/htmlcharset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
var i = 0
function changeColor(){
var color = new Array("red","orange","yellow","green","blue","indigo","purple")
var message = new Array("红","橙","黄","绿","蓝","靛","紫")
document.getElementById("color").style.color = color[i]
document.getElementById("message").innerHTML = message[i]
i++
if(i==color.length){
i = 0
}
}
setInterval(changeColor,1000)
onload = changeColor
</script>
</head>
<body>
<h1 id="color" style="color:red">彩虹的颜色</h1>
<h1 id="message">红</h1>
</body>
</html>