1. 程式人生 > >JS中用function和沒有用function的區別

JS中用function和沒有用function的區別

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		#div1 {
			width: 100px;
			height: 100px;
			background: red;
		}
		 
	</style>
	<script type="text/javascript">
	function  toGreen()
	{
		document.getElementById('div1').style.width='200px';document.getElementById('div1').style.height='200px';document.getElementById('div1').style.background='green';
	}
	function toRed() {
		document.getElementById('div1').style.width='100px';document.getElementById('div1').style.height='100px';document.getElementById('div1').style.background='red';
	}
	</script>
</head>
<body>
	<div id="div1" onmouseover="toGreen()" onmouseout="toRed()">

	</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		#div1 {
			width: 100px;
			height: 100px;
			background: red;
		}
		 
	</style>
</head>
<body>
	<div id="div1" onmouseover="document.getElementById('div1').style.width='200px';document.getElementById('div1').style.height='200px';document.getElementById('div1').style.background='green';" onmouseout="document.getElementById('div1').style.width='100px';document.getElementById('div1').style.height='100px';document.getElementById('div1').style.background='red';">

	</div>
</body>
</html>