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
How to get the jQuery element (or attributes) in a click event in Backbone.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-name
attribute of the link that was clicked inside of the example_event
function ?
如何获取data-name
在example_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');
}