js綁定事件的經常使用方式

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JS事件綁定</title>
<script type="text/javascript">
/**
 * 綁定事件的方式:用事件屬性綁定事件函數
 * 優勢:1:完成行爲的分離 
 * 2:便於操做當事對象,由於function是做爲on***的屬性出現的,可直接用this引用當事對象。
 * 3:方便讀取事件對象,事件觸發時系統自動把事件對象傳遞給事件函數,已其一個來傳
 */	
   window.onload=function(){
   	  var k=document.getElementById('k').onclick=function(event){
	  var jj=document.getElementById('jj');
	     jj.style.top=event.clientX+'px';
		 jj.style.left=event.clientY+'px';
	  }
   } 
   
</script>
<style>
	#k{width:60px;height:80px; background-color:#80ffff;}
	#jj{width:60px ;height:80px;background-color:#ffff00;z-index:1000;position:absolute;}
</style>
</head>
<body>
<div id="k"></div>
<div id="jj"></div>	
</body>
</html>
相關文章
相關標籤/搜索