本文共 807 字,大约阅读时间需要 2 分钟。
You can do so by adding an event listener to the document
object for the DOMContentLoaded
event:
您可以通过将事件监听器添加到DOMContentLoaded
事件的document
对象中来实现:
document.addEventListener('DOMContentLoaded', (event) => { //the event occurred})
I usually don’t use arrow functions inside for the event callback, because we cannot access this
.
我通常不在事件回调中使用箭头函数,因为我们无法访问this
。
In this case we don’t need so, because this
is always document
. In any other event listener I would just use a regular function:
在这种情况下,我们不需要这样做,因为this
始终是document
。 在任何其他事件侦听器中,我都将使用常规函数:
document.addEventListener('DOMContentLoaded', function(event) { //the event occurred})
for example if I’m adding the event listener inside a loop and I don’t really know what this
will be when the event is triggered.
例如,如果我加入一个循环内的事件监听器,我真的不知道this
在触发事件会。
翻译自:
转载地址:http://moqgb.baihongyu.com/