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 ?
如何获取在example_event函数中单击的链接的data-name属性?
2 个解决方案
#1
41
Try this.
尝试这个。
example_event : function(event) {
//need to get the data-name here
var name = $(event.target).data('name');
}
#2
17
You can also do this without jQuery using JavaScript's getAttribute method:
您也可以使用JavaScript的getAttribute方法在没有jQuery的情况下执行此操作:
example_event : function(event) {
//need to get the data-name here
var name = event.target.getAttribute('data-name');
}
#1
41
Try this.
尝试这个。
example_event : function(event) {
//need to get the data-name here
var name = $(event.target).data('name');
}
#2
17
You can also do this without jQuery using JavaScript's getAttribute method:
您也可以使用JavaScript的getAttribute方法在没有jQuery的情况下执行此操作:
example_event : function(event) {
//need to get the data-name here
var name = event.target.getAttribute('data-name');
}