繁体   English   中英

如何在Backbone.js中的click事件中获取jQuery元素(或属性)?

[英]How to get the jQuery element (or attributes) in a click event in Backbone.js?

我有以下代码:

HTML

<div id='example'>
 <a href='#' data-name='foo' class='example-link'>Click Me</a>
 <a href='#' data-name='bar' class='example-link'>Click Me</a>
</div>

JavaScript的

example_view = Backbone.View.extend({
    el: $("#example"),
    events: {
      'click .example-link' : 'example_event'
    },
    example_event : function(event) {
      //need to get the data-name here
    } 
});

如何获取在example_event函数中单击的链接的data-name属性?

试试这个。

example_event : function(event) {
  //need to get the data-name here
  var name = $(event.target).data('name');
} 

您也可以使用JavaScript的getAttribute方法在没有jQuery的情况下执行此操作:

example_event : function(event) {
  //need to get the data-name here
  var name = event.target.getAttribute('data-name');
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM