Javascript 基礎教學之事件小實例

Javascript 事件的小實例:

#透過點擊按鈕讓div的顏色改變

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>事件</title>
	<style type="text/css"> div{width:150px;height:150px;border:1px solid #f60;}</style>	
</head>
<body>
		<div id="dv"></div><br>

		<input type="button" value="点击" onclick="bt()">

		<script type="text/javascript">
			function bt(){
				var div=document.getElementById("dv");
	        	div.style.backgroundColor="red";
			}
		</script>
</body>
</html>


#
繼續學習
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>事件</title> <style type="text/css"> div{width:150px;height:150px;border:1px solid #f60;}</style> </head> <body> <div id="dv"></div><br> <input type="button" value="点击" onclick="bt()"> <script type="text/javascript"> function bt(){ var div=document.getElementById("dv"); div.style.backgroundColor="red"; } </script> </body> </html>