2

燃えさしのImスターター。attritube "name"のリンクがたくさんありますが、そのうちの1つをクリックすると、この属性を取得したいと思います。私は1つとbindAttrだけを操作する方法を知っていますが、さらにこのコードを試してみましたが、機能していません。すべてのリンクに多くのbindAttr???を使用する必要がありますか?昨日これを作成しました(1つのリンクのみ):

<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>
4

1 に答える 1

3

これは、を使用して実現できevent.currentTargetます。そのjQueryプロパティ

説明:イベントバブリングフェーズ内の現在のDOM要素。

このプロパティは通常、関数のthisと同じになります。

jQuery.proxyまたは別の形式のスコープ操作を使用している場合、これは、event.currentTargetではなく、指定したコンテキストと同じになります。

currentTargetjQueryのドキュメントでは、はに等しい必要があると述べているため、Emberはイベントに対して特別なコンテキストを作成していると思いますthis

したがって、コードでは次のようになります。

clicked: function(event) {
    console.log($(event.currentTarget).attr('name'));
}

このJSFiddleでこのソリューションを試すことができます。

于 2012-10-07T19:07:15.897 に答える