我是 Ember 的新手。我有很多链接(即),带有属性“名称”,并且我想在单击其中一个链接时获得此属性。我知道如何仅使用一个和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>
最佳答案
您可以通过使用event.currentTarget
来实现这一点。它是jQuery property :
Description: The current DOM element within the event bubbling phase.
This property will typically be equal to the this of the function.
If you are using jQuery.proxy or another form of scope manipulation, this will be equal to whatever context you have provided, not event.currentTarget
我猜 Ember 为事件做了一些特殊的上下文,因为 jQuery 文档说 currentTarget
应该等于 this
。
所以它在你的代码中看起来像这样:
clicked: function(event) {
console.log($(event.currentTarget).attr('name'));
}
您可以尝试这个解决方案 in this JSFiddle .
关于attributes - ember,如何在多个链接中获取 "name"属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12769802/