vertebral.js-eventos, saber qué se hizo clic


En uno de mis pilares.js ver clases, tengo algo como:

...

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

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

...

Porque mi marcado por página podría tener algo como:

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

Entonces, ¿cómo puedo encontrar exactamente información sobre el elemento que causó el evento? O en este caso, que se hizo clic?

Author: Matthew, 2011-04-15

3 answers

Normalmente en un enlace de evento, solo usarías $(this), pero estoy bastante seguro de que las vistas Backbone están configuradas para que this siempre se refieran a la vista, así que prueba esto:

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

EDITAR REALMENTE TARDE : Probablemente quieras usar $(ev.currentTarget). Véase la discusión sobre la respuesta de pawlik a continuación

 130
Author: Jamie Wong,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-04 20:02:36

ev.target puede ser engañoso, debe usar ev.currentTarget como se describe en http://www.quirksmode.org/js/events_order.html

 96
Author: pawlik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-04-15 23:06:40

Puede obtener cualquier atributo que desee. ev funciona como this:

perpage: function(ev) {
        console.log($(ev.target).attr('name'));
}
 0
Author: Nvan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-20 14:22:07