问题描述
我找不到这样做的区别:
I can't find the difference between doing:
window.onscroll = () => console.log("scroll")
还有这个:
window.addEventListener('scroll', () => console.log("scroll"))
除了浏览器兼容性,这两者似乎在大多数 IE 版本中都不支持!
except for browser compatibility which both seems to be unsupported in most IE versions !
这只是语法上的差异吗?使用 removeEventListener
删除处理程序似乎很简单,但我假设 window.onscroll = null
具有类似的效果.
is it just a syntax difference? it seems that it is straightforward to remove the handler using removeEventListener
, but I'm assuming window.onscroll = null
has similar effect.
我错过了什么吗?
推荐答案
主要区别在于只能有一个 onscroll
.您添加的任何其他 onscroll
将覆盖第一个,而您可以根据需要拥有任意数量的 .addEventListener('scroll', ()=>{})
.
The main differences is that there can only be one onscroll
. Any additional onscroll
s you add will override the first, whereas you can have as many .addEventListener('scroll', ()=>{})
as you want.
这篇关于window.onscroll 和 window.addEventListener 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!