gpt4 book ai didi

jquery $(this) 在 CoffeeScript /主干中不起作用

转载 作者:行者123 更新时间:2023-11-30 23:49:02 25 4
gpt4 key购买 nike

我最近开始使用 Brunch 来玩 Backbone 和 CoffeeScript,并且想知道为什么会出现这样的情况......

events: {
"click .button" : "open",
"hover .info" : "hover"
},

hover: =>
$(this).css("background-color", "#333")

..不起作用。

据我了解,CoffeeScript 有自己的 this 版本,它可能与 jQuery 使用的内容冲突,但在文档中我认为 => 将其绑定(bind)到当前对象。我也尝试过 -> 也无济于事。知道为什么这不起作用吗?

HTML:

<div id='outer'> 
<div class='.info'> <a href='google.com'> google </a> </div>
<div class='.info'> <a href='google.com'> google </a> </div>
<div class='.info'> <a href='google.com'> google </a> </div>
</div>

最佳答案

来自the docs :

All attached callbacks are bound to the view before being handed off to jQuery, so when the callbacks are invoked, this continues to refer to the view object.

如果 this 是 View 对象(而不是 HTML 元素),则 $(this) 毫无意义。我相信,您想要做的是传递 View 引用的 元素 $,例如:

hover: =>
$(this.el).css("background-color", "#333")
# -----^

关于jquery $(this) 在 CoffeeScript /主干中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7670380/

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