<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#box{width:200px;height:200px;background:pink;margin:30px;}
#div1{width:200px;height:200px;background:lightgreen;margin:30px;}
</style>
</head>
<body>
<div id="box" onclick="test(event,this)"></div>
<div id="div1"></div>
</body>
<script>
//标签引入的方式绑定事件对象 获取事件对象 需要在绑定事件的时候传递一个实参为event对象 函数使用一个形参来接受即可
function test(e,obj){
//alert(e);
document.title="X:"+e.clientX+"Y:"+e.clientY;
//alert(obj);
obj.innerHTML = '男神一样的***---帅想想';
}
//标签绑定
var div1 = document.getElementById('div1');
div1.onclick=function(e){
//iE可以直接使用事件对象event 这个对象
//alert(event);
//alert(e);
//解决兼容性问题
// if(e == undefined){
// //IE
// e = event;
// }
// 调整兼容性问题
var e = e || event;
//alert(e);
document.title="X:"+e.clientX+'Y:'+e.clientY;
//div1.innerHTML = '狗哥是个大帅哥';
this.innerHTML = '琦琦是个大美女';
}
</script>
</html>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号