<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<button>按钮1</button>
<button>按钮2</button>
<script>
let obj={
site:"中华人民共和国",
handleEvent:function(event){
console.log(this.site+event.target.innerHTML);
},
show:function(){
const _this=this;
const button=document.querySelectorAll("button");
for (const but of button) {
but.addEventListener("click",this);
}
}
}
obj.show();
</script>
</body>
</html>
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/502626116336175.png "=600 #left")
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<button>按钮1</button>
<button>按钮2</button>
<script>
let obj={
site:"中华人民共和国",
handleEvent:function(event){
console.log(this.site+event.target.innerHTML);
},
show:function(){
const _this=this;
const button=document.querySelectorAll("button");
button.forEach(function(elem){
elem.addEventListener("click",this);
})
}
}
obj.show();
</script>
</body>
</html>
为什么用for of就可以打印出内容,foreach就不行呢?