在Backbone中,很明显如何制作点击事件,但是我很难获得表示事件绑定的选择器的实际元素。
我是否必须检查是否拥有它或通过$(ev.target).parent()
上传DOM,还是有更简单的方法?
标记示例:
<div data-action="handle">
<div>This is the click event target.</div>
</div>
骨干视图示例:
Backbone.View.extend({
events: {
'click [data-action="handle"]': 'handle'
},
handle: function(ev) {
// ev.target doesnt match up with the actual selector above
// How do I get $element such that:
// $element.data('action') === 'handle'
}
}
答案 0 :(得分:2)
主干使用事件委托。如果你想尝试
ev.target
它将为您提供事件实际发生的元素,即内部DIV。
要获得上述工作用途: -
ev.currentTarget
这将为您提供实际附加事件的元素。
答案 1 :(得分:0)
另一个答案是,通过this
可以获取捕捉该事件的元素。
如果您在回调中使用了bindAll