字符串类型的转换之toString()示例

来源:互联网 发布:易到打车软件 编辑:程序博客网 时间:2024/05/21 07:58

用toString()以及简单的JS for循环知识,在网页输出100个小正方形,并且可以点击变色。

<!DOCTYPE html>
<html>


<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="5">
<title></title>
<style type="text/css">
* {
margin: 0px;
padding: 0px;
}

.fu{
height: 100px;
width: 100px;
float: left;
}
#zhong{
margin:0px auto;
width: 1000px;
border:2px red solid;
height: 1000px;
}
</style>

</head>


<body>

<div id="zhong" onclick="dianji()">

<script type="text/javascript">


var by = document.getElementById("zhong");
document.onclick = function dian() {
by.innerHTML = '';
for(var b = 1; b <= 100; b++) {
var c = "#"
for(var a = 1; a <= 6; a++) {
var num = parseInt(Math.random() * 16);
num = num.toString(16);
c = c + num;
}
by.innerHTML+= "<div class='fu'  style='background-color:" + c + "'></div>";

                                   }
}

</script>
<div style="clear:both;"></div><!--清除浮动-->

</div>

</body>

</html>

不懂的请留言,看到马上回复

原创粉丝点击