复制代码代码如下:
<! Doctype html public "-// w3c // dtd xhtml 1.0 transisi // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv = "konten tipe" content = "text /html; charset = utf-8" />
<type style = "text/css">
tubuh{
font-size: 12px;
Font-Family: "Courier New", Courier, Monospace;
spasi surat: 5px;
}
ul {
List-style: tidak ada;
}
Li {
Lebar: 130px;
Tinggi: 130px;
Line-Height: 130px;
Vertikal-Align: tengah;
Teks-Align: tengah;
float: kiri;
margin-kiri: 20px;
}
</tyle>
<skrip src = "http://code.jquery.com/jquery-1.11.0.min.js"> </script>
<title> javascript 生成随机颜色 </iteme>
<type skrip = "Teks/JavaScript">
$ (function () {
$ ("ul li"). masing -masing (fungsi () {
$ (ini) .css ("latar belakang-warna", getRandomColor ());
});
})
fungsi getRandomColor ()
{
var c = '#';
var carray = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
untuk (var i = 0; i <6; i ++)
{
var cindex = math.round (math.random ()*15);
c += carray [cindex];
}
kembali C;
}
</script>
</head>
<body>
<div>
<ul>
<li> 第一个色块 </li>
<li> 第二个色块 </li>
<li> 第三个色块 </li>
<li> 第四个色块 </li>
</ul>
</div>
</body>
</html>