代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>数学对象实例</title>
<style type="text/css">

</style>
<script type="text/javascript">
function changeColor(){
var red = Math.ceil(Math.random()*255);
var green = Math.ceil(Math.random()*255);
var blue = Math.ceil(Math.random()*255);
var color = "#"+red.toString(16)+green.toString(16)+blue.toString(16);
document.getElementById("content").style.color=color;
document.getElementById("content").style.fontWeight="bold";
}
</script>
</head>
<body>
<p id="content">我要变颜色!</p>
<input type="button" value="变换颜色" οnclick="changeColor()"/>

</body>
</html>

注意:


Math.random()随机0-1的数字

id.style.方法用于设置外观

javascript小练习实现颜色随机变化_javascript