宗策

导航

JavaScript生成div效果

 

页面效果:

  

 

 脚本展示:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>Insert title here</title>
<style type="text/css">
	.yellow{
		width: 100px;
		height: 100px;
		background-color: yellow;
		float:left;
	}
	.green{
		width: 100px;
		height: 100px;
		background-color: green;
		float:left;
	}
	div{
	
	}
</style>
<script type="text/javascript">
	function myfn(color){
		var d = document.getElementById("d1");
		d.innerHTML+="<div class='"+color+"'></div>";
	}
</script>
</head>
<body>
<input type="button" value="黄色" onclick="myfn('yellow')">
<input type="button" value="绿色" onclick="myfn('green')">
<div id="d1"></div>
</body>
</html>

  

posted on 2020-05-06 11:20  宗策  阅读(233)  评论(0编辑  收藏  举报