Backbone.js 视图 - 将事件绑定到“el"之外的元素; [英] Backbone.js views - binding event to element outside of "el"

查看:23
本文介绍了Backbone.js 视图 - 将事件绑定到“el"之外的元素;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的第二个答案很好地解释了如何Backbone.js 视图中的事件声明的范围是视图的 el 元素.

The 2nd answer to this question nicely explains how event declarations in Backbone.js views are scoped to the view's el element.

想要将事件绑定到 el 范围之外的元素似乎是一个合理的用例,例如页面不同部分的按钮.

It seems like a reasonable use case to want to bind an event to an element outside the scope of el, e.g. a button on a different part of the page.

实现这一目标的最佳方法是什么?

What is the best way of achieving this?

推荐答案

更新:此答案不再有效/正确.请参阅下面的其他答案!

你为什么要这样做?

除此之外,您始终可以使用常规 jQuery 处理程序绑定它.例如

Apart from that, you could always just bind it using regular jQuery handlers. E.g.

$("#outside-element").click(this.myViewFunction);

IIRC,Backbone.js 只使用常规的 jQuery 处理程序,所以您本质上在做同样的事情,但打破了范围:)

IIRC, Backbone.js just uses the regular jQuery handlers, so you're essentially doing the same thing, but breaking the scope :)

这篇关于Backbone.js 视图 - 将事件绑定到“el"之外的元素;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆