js改变div样式

原创 2018-12-03 11:22:49 443
摘要:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>js控制div样式</title> <style type="text/css"> #box{ w
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>js控制div样式</title>
		<style type="text/css">
			#box{
				width: 100px;
				height: 100px;
				background: red;
				margin: 20px 80px;
			}
		</style>
	</head>
	<body>
		<div id="box"></div>
		<input type="button" name="" id="" value="改变高度" onclick="change(value)"/>
		<input type="button" name="" id="" value="改变宽度" onclick="change(value)"/>
		<input type="button" name="" id="" value="改变颜色" onclick="change(value)"/>
		<input type="button" name="" id="" value="重置" onclick="change(value)"/>
		<input type="button" name="" id="" value="隐藏" onclick="change(value)"/>
		<input type="button" name="" id="" value="显示" onclick="change(value)"/>
		<script type="text/javascript">
			var box=document.getElementById("box");
			function change (value) {
				if (value=="改变高度") {
					box.style.height="400px";
				} else if(value=="改变宽度"){
					box.style.width="400px";
				} else if (value=="改变颜色") {
					box.style.backgroundColor="pink";
				} else if(value=="重置"){
					box.style.height="100px";
					box.style.width="100px";
					box.style.background="red";
				} else if (value=="隐藏") {
					box.style.display="none";
				} else if (value=="显示") {
					box.style.display="block";
				}
			}

		</script>
	</body>
</html>


批改老师:天蓬老师批改时间:2018-12-03 13:45:54
老师总结:用事件侦听器,改写一下这个案例, addEventListener(), 试试看

发布手记

热门词条