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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10518411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:40:43  来源:igfitidea点击:

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

javascriptjquerybackbone.js

提问by GSto

I have the following code:

我有以下代码:

HTML:

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

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
    } 
});

how can I get the data-nameattribute of the link that was clicked inside of the example_eventfunction ?

如何获取data-nameexample_event函数内部单击的链接的属性?

回答by ShankarSangoli

Try this.

尝试这个。

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

回答by tomsabin

You can also do this without jQuery using JavaScript's getAttributemethod:

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

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