我是Ember的首发。我有许多链接(即),带有attritube“name”,当我点击其中一个时,我想获得这个属性。我知道如何只使用一个和bindAttr,但更多我正在尝试使用此代码,但无法正常工作。我需要使用很多bindAttr ???,对于每个链接?昨天我做了这个(只有一个链接):
<body>
<div id="templateHere"></div>
<!--handlebar-->
<script type="text/x-handlebars" data-template-name="text">
<h1>Send the message:</h1>
<a {{action "clicked" on="click"}} name='link1'>Click me!!</a>
<a {{action "clicked" on="click"}} name='link2'>click me again</a>
</script>
<script>
//namespace
App = Ember.Application.create();
//define view
App.myview = Ember.View.extend({
templateName: 'text',
name_attribute:'name_buttooooon',
message: '',
clicked: function(event) {
console.log(jQuery(this).attr('name'));//get attribute name value
}
});
//create view
App.myview=App.myview.create();
//insert view in body
$(function() {
App.myview.append('#templateHere');
});
</script>
</body>
答案 0 :(得分:3)
您可以使用event.currentTarget
来实现此目的。它是jQuery property:
描述:事件冒泡阶段中的当前DOM元素。
此属性通常等于函数的属性。
如果您使用jQuery.proxy或其他形式的范围操作,这将等于您提供的任何上下文,而不是event.currentTarget
我猜Ember为事件制作了一些特殊的上下文,因为jQuery文档说currentTarget
应该等于this
。
所以在你的代码中看起来像这样:
clicked: function(event) {
console.log($(event.currentTarget).attr('name'));
}
您可以尝试此解决方案in this JSFiddle。