0

在 Backbone 中,如何制作点击事件非常明显,但我很难获得表示事件绑定到的选择器的实际元素。

我是否必须检查我是否拥有它或通过 DOM 上去$(ev.target).parent(),还是有更简单的方法?

标记示例:

<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'
  }
}
4

2 回答 2

2

Well the backbone uses event delegation. If you will try to get

ev.target

it will get you the element where the event actually occurs i.e. the inner DIV.

To get the above working use:-

ev.currentTarget

This will give you the element where you have actually attached the event.

于 2013-06-05T15:23:54.363 回答
0

另一个答案是捕获事件的元素可以通过this.

bindAll但是,如果您在回调中使用,则不会出现这种情况

于 2015-02-23T15:55:43.863 回答