js绑定事件的常用方式

来源:互联网 发布:零基础数据库培训 编辑:程序博客网 时间:2024/05/17 22:58
<?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>

0 0
原创粉丝点击