jquery $(this) 无法在咖啡脚本/骨干中工作

发布于 2024-12-08 02:56:55 字数 731 浏览 0 评论 0原文

我最近开始使用 Brunch 来玩 Backbone 和 CoffeeScript,并且想知道为什么这样的东西.....

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

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

不起作用。

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

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>

I recently started playing with Backbone and CoffeeScript using Brunch and was wondering why something like this...

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

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

..does not work.

From my understanding CoffeeScript has its own version of this which could conflict with what jQuery uses but in the documentation I thought => binds it to the current object. I have also tried -> as well to no avail. Any idea on why this doesn't work?

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

荭秂 2024-12-15 02:56:55

来自文档

所有附加的回调在传递给 jQuery 之前都绑定到视图,因此当调用回调时,this 继续引用视图对象。

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

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

From 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.

And if this is the view object (rather than, say, an HTML element), $(this) is fairly meaningless. What you want to do, I believe, is pass the element to which the view refers to $, e.g.:

hover: =>
  $(this.el).css("background-color", "#333")
  # -----^
甜心 2024-12-15 02:56:55

乔丹的答案在这方面是正确的 - 如果你想要视图的元素使用 this.el。

在您的情况下,您需要触发事件的元素,即 .info 元素。这可以通过 event.currentTarget1 检索

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

Jordan's answer is correct with respect to this - if you want the view's element use this.el.

In your case you want the element that triggered the event i.e. the .info element. This can be retrieved through event.currentTarget1

hover: (e) =>
  $(e.currentTarget).css("background-color", "#333")
浪漫之都 2024-12-15 02:56:55

Jordan 和gingerhendrix 都是正确的,但让我退一步考虑一下 =>-> 的更普遍的问题。

毫无疑问,您已经看过/使用过诸如“

$('#foo').hover ->
  $(this).css('background-color', '#333')

为什么这有效?” 之类的代码。好吧,在内部,jQuery 的悬停函数看起来像这样[显然,高度风格化]:

hover: (callback) ->
  for el in @
    el.onmouseenter = el.onmouseleave = (e) -> callback.call el, e

callback.call el, e 就像调用 callback(e) 一样,只不过它使 this 指向该函数调用内部的 el。这就是为什么 $(this) 为您提供了一个包裹着悬停事件所在元素的 jQuery 对象。

但是,如果您这样写,这是行不通的,

$('#foo').hover => ...

因为 => 覆盖 callapply;它强制 this 始终表示相同的意思,无论函数如何调用。

事情是这样的:Backbone 的 hover 包裹了 jQuery 的 hover,使得 this 无论如何都指向 View 实例...所以它实际上并没有根据您的情况,使用 -> 还是 => 并不重要。这就是为什么他们在文档$(this.el)一个>。这很棒,因为您几乎肯定希望在处理事件时访问视图的属性。正如gingerhendrix 指出的那样,this.el 将为您提供整个 View 元素,而不是接收 hover 事件的特定元素;但事件对象 e 拥有您所需要的(以及更多)。

有关绑定函数运算符的更多信息,查看我的书

Jordan and gingerhendrix are both correct, but let me step back a moment to consider the more general question of => vs. ->.

You've no doubt seen/used code like

$('#foo').hover ->
  $(this).css('background-color', '#333')

Why does this work? Well, internally, jQuery's hover function looks something like this [heavily stylized, obviously]:

hover: (callback) ->
  for el in @
    el.onmouseenter = el.onmouseleave = (e) -> callback.call el, e

callback.call el, e is just like calling callback(e), except that it makes this point to el inside of that function call. So that's why $(this) gives you a jQuery object wrapped around the element that the hover event is on.

But this wouldn't work if you wrote

$('#foo').hover => ...

because => overrides call and apply; it forces this to always mean the same thing, no matter how the function is called.

Here's the thing: Backbone's hover wraps around jQuery's hover in such a way that this points to the View instance anyway... so it actually doesn't matter in your case whether you use -> or =>. That's why they use $(this.el) in the docs. And that's great, because you'll almost certainly want to have access to the view's properties when handling events. As gingerhendrix pointed out, this.el is going to give you the overall View element, not the specific element receiving the hover event; but the event object e has what you need (and so much more).

For more on the bound function operator, check out my book.

没︽人懂的悲伤 2024-12-15 02:56:55

好吧,$(this) 对我不起作用,所以我做了 $(event.target)。这肯定会起作用。

Well $(this) is not working me to so I did $(event.target). This will work for sure.

深海夜未眠 2024-12-15 02:56:55

在使用 Coffee Script 时,我像这样编写 jQuery 调用:

$ -> $('#entryBox').val "Placeholder text"

我什至还没有费心去弄清楚它为什么有效,但它确实有效。

I write my jQuery calls like this when using Coffee Script:

$ -> $('#entryBox').val "Placeholder text"

I haven't even bothered figuring out why it works yet, but it does work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文