backbone.js - events, knowing what was clicked

前端 未结 3 922
一生所求
一生所求 2021-01-29 20:49

In one of my backbone.js view classes, I have something like:

...

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

perpage: function() {
  // Access the          


        
相关标签:
3条回答
  • 2021-01-29 21:26

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

    0 讨论(0)
  • 2021-01-29 21:26

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

    perpage: function(ev) {
            console.log($(ev.target).attr('name'));
    }
    
    0 讨论(0)
  • 2021-01-29 21:43

    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

    0 讨论(0)
提交回复
热议问题