javascript小练习实现颜色随机变化
原创
©著作权归作者所有:来自51CTO博客作者徐的张的原创作品,请联系作者获取转载授权,否则将追究法律责任
代码如下:
<!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>
注意:
id.style.方法用于设置外观