这里写自定义目录标题
JavaScript 在addEventListener() 方法中传其他参数
解决方法:
1.方法. bind(参数)
2,在方法内部用this获取
注意方法中只含一个参数 e
// An highlighted block
const fun = function(e){
console.log(this)
}
tag.addEventListener('mouseover', fun.bind(0.5));
// this = 0.5
同理可以传多个参数,使用对象或者数组
// An highlighted block
const fun = function(e){
console.log(this.name)
}
tag.addEventListener('mouseover', fun.bind({name:'tom',age:10}));
// this = 'tom'
本文介绍了在JavaScript中使用addEventListener()方法时如何传递额外参数。提供了两种解决方案:使用bind()方法绑定参数,或者在方法内部通过this获取参数。强调了在事件处理函数中通常只有一个参数e,但可以通过对象或数组传递多个参数。
922

被折叠的 条评论
为什么被折叠?



