gpt4 book ai didi

javascript - 带有重新加载的 jquery 单击事件

转载 作者:行者123 更新时间:2023-11-30 20:33:34 25 4
gpt4 key购买 nike

我想知道为什么这段代码:

$('.button').click(window.location.reload)

触发非法调用

还有这段代码:

$('.button').click(function(){
window.location.reload()
})

工作正常。

我不知道区别,为什么只有后者有效?

最佳答案

reload() 引用了 this。在工作示例中,this 指向 window.location。在您尝试的较短版本中,this 指向 window

这会起作用:

$('.button').click(window.location.reload.bind(window.location));

这里,window.location 被绑定(bind)为 reload() 的上下文。

关于javascript - 带有重新加载的 jquery 单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50075820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com