簡體   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