Backbone js-events 知道点击了什么

在我的一个 backbone. js view 类中,我有这样的东西:

...


events: {
'click ul#perpage span' : 'perpage'
},


perpage: function() {
// Access the text of the span that was clicked here
// Something like: alert($(element).text())
},


...

因为我的每页标记可能有这样的内容:

<ul id="perpage">
<li><span>5</span></li>
<li><span>10</span></li>
</ul>

那么我怎样才能确切地找到关于引起事件的元素的信息呢?或者在这种情况下,被点击了?

44944 次浏览

Normally on an event bind, you would just use $(this), but I'm fairly sure Backbone views are set up so that this always refer to the view, so try this:

perpage: function(ev) {
alert($(ev.target).text());
}

REALLY LATE EDIT: You probably want to use $(ev.currentTarget). See dicussion on pawlik's answer below

ev.target can be misleading, you should use ev.currentTarget as described on http://www.quirksmode.org/js/events_order.html

You can get any attribute you want. ev works as this:

perpage: function(ev) {
console.log($(ev.target).attr('name'));
}