获取点击元素的属性

时间:2013-10-08 09:53:30

标签: ember.js

我在模板中有以下内容

<a href="javascript:void(0)" tab-name="Personal Information" {{action "switchTab" target="view"}}>Personal Information</a>

我的观点如下

App.SidebarView = Ember.View.extend({
    templateName: 'sidebar',
    actions: {
        switchTab: function(e){
             //Some Code
        }
    }
});

如何在点击

时获取tab-name属性

2 个答案:

答案 0 :(得分:1)

你只需要捕获一个与DOM相关的事件,比如click来获取事件对象,在action内没有传递事件对象,所以尝试将你的视图更改为:

在您的模板中删除action

<a href="javascript:void(0)" tab-name="Personal Information">Personal Information</a>

然后在click事件的视图注册表中:

App.SidebarView = Ember.View.extend({
  templateName: 'sidebar',
  click: function(e){
    var tabName = $(e.target).attr("tab-name");
    console.log(tabName);
  }
});

示例jsbin:http://jsbin.com/ETUroGA/1/edit

希望它有所帮助。

答案 1 :(得分:-1)

我认为你可以这样做:

App.SidebarView = Ember.View.extend({
    templateName: 'sidebar',
    actions: {
        switchTab: function(event, view){
            var tab-name = $(event.target).attr("tab-name")
            // some code
        }
    }
});
相关问题