<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>解决浏览器事件绑定兼容性问题</title>
<style>
.div1 {
width: 200px;
height: 200px;
background-color: paleturquoise;
}
.div2 {
margin: 25px auto;
width: 100px;
height: 100px;
background-color: orange;
}
</style>
</head>
<body>
<div class="div1">div1
<div class="div2">div2</div>
</div>
<script>
var div1=document.querySelector('.div1');
var div2=document.querySelector('.div2');
function func () {
console.log('这是div');
}
// function func2 () {
// console.log('IE里的div');
// }
var cleanBug={
add:function (ele,type,func) {
if (ele.addEventListener) {
ele.addEventListener(type,func);
}else {
ele.attachEvent('on'+type,func);
}
},
remove:function (ele,type,func) {
if (ele.removeEventListener) {
ele.removeEventListener(type,func);
}else {
ele.detachEvent('on'+type,func);
}
}
}
cleanBug.add(div1,'click',func);
// (div2,'click',func);
</script>
</body>
</html>
老师,我问两个问题:
1、如果两个div是嵌套关系,我给外面的div敲击事件,里面的div点击它也会产生敲击事件?
2、我想给div1和div2各一个敲击事件且让他们打印的值不一样,代码该怎么修改?