实训篇-JavaScript-陶渊明去没去过桃花源

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<!--定义样式-->
		<style type="text/css">
			/*id选择器 定义div的大小 背景色*/
			#div1{
				 300px;
				height: 200px;
				background-color:cornflowerblue;
				border: solid 1px black;
				/*上边距*/
				margin-top: 50px;
				/*左边距*/
				margin-left: 600px;
				text-align: center;
				
			}
			/*元素选择器*/
			input{
				 70px;
				height: 50px;
				margin-left: 20px;
				color: red;
			}
		</style>
		<script type="text/javascript">
			//随机生成div新的位置
			function random(){
				//得到div
				var div1=document.getElementById("div1");
				//随机得到两个边距的值
				var left=Math.random()*1200;
				var top=Math.random()*500;
				div1.style.marginLeft=left+"px";
				div1.style.marginTop=top+"px";
				
			}
			//隐藏div并设置页面的背景图片
			function test(){
				//设置body的背景图片
				document.body.style.backgroundImage="url(img/background.jpg)"
				//隐藏
				var div1=document.getElementById("div1");
				div1.style.display="none";
			}
		</script>
	</head>
	<body>
		<!--块-->
		<div id="div1">
			陶渊明去没去过桃花源?
			<br /><br />
			<input type="button" name="" id="" value="去过" onclick="test()" />
			<!--当鼠标的位置 移动到‘没去过’按钮的时候 触发方法 重新生成div的位置-->
			<input type="button" name="" id="" value="没去过"  onmouseover="random()"/>
		</div>
	</body>
</html>

  

原文地址:https://www.cnblogs.com/52dxer/p/12165192.html