主要知识点有:

  1. 数组的两种新建方式,
  2. Math.random函数的运用
  3. innerHTML与 += 运算符的运用


<!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>豪情</title>
</head>
<body>
<span id="t"></span>
<script type="text/javascript">
var texts = new Array('中国', '西安', '上海', '中国', '西安', '上海');
var colors = ['red', 'blue', 'gray', 'orange'];
for(var i=0; i<texts.length; i++){
//alert(parseInt(Math.random()*colors.length));
document.getElementById('t').innerHTML += "<span style='font-weight:bold;margin:0 10px;color:" + colors[parseInt(Math.random()*colors.length + 1)] + ";'>" + texts[i] + "</span>";
}
</script>
</body>
</html>
运行代码