码迷,mamicode.com
首页 > 编程语言 > 详细

javascript小练习—点击将DIV变成红色(通过for循环遍历)

时间:2015-05-19 10:19:03      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>点击将DIV变成红色</title>
<style>
body{font:12px/1.5 Tahoma;text-align:center;}
code,input,button{font-family:inherit;}
#div{width: 1000px;height: 240px;position: relative;}
#div div{width: 200px;height: 200px;background-color: black;position: relative;float: left;margin: 10px;}
button{cursor:pointer;}
</style>
<script>
window.onload = function(){
var oDiv = document.getElementById("div").getElementsByTagName("div");
var oButton = document.getElementsByTagName("button")[0];
oButton.onclick = function(){
for(var i = 0;i<oDiv.length;i++){
oDiv[i].style.backgroundColor = "red";
}
};
};
</script>
</head>
<body>
<div id="div">
<div></div>
<div></div>
<div></div>
</div>
<p><button>点击将DIV变成红色</button></p>
</body>
</html>

javascript小练习—点击将DIV变成红色(通过for循环遍历)

标签:

原文地址:http://www.cnblogs.com/zhaoyixin/p/4513636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!