0

私が次のものを持っているとしましょうdiv

<div id="123" class="abc">Foobar</div>

divこれがマウスオーバーされたときに関数をトリガーするためにこれを行うことができることを私は知っています:

$(".abc").bind({
    mouseenter : SomeFunction(id)
});

これにより、実行する前に、「123」であるSomeFunctionthis の id を抽出し、それをパラメーターとして渡して処理できるようにしたいと考えています。私はそれを行うことができますか?divSomeFunction

4

6 に答える 6

4
$('.abc').bind('mouseenter', function() {
    SomeFunction($(this).attr('id'));
});

または、本当にイベント マップの構文が必要な場合は、次のようにします。

$('.abc').bind({
    mouseenter: function() {
        SomeFunction($(this).attr('id'));
    }
});
于 2012-05-14T18:37:25.910 に答える
2
$(".abc").bind('mouseenter', function() {
  var id = this.id;
})

あなたの質問によると

$(".abc").bind({

 mouseenter : function() {
                SomeFunction(this.id)
              }

});
于 2012-05-14T18:38:44.933 に答える
2
$(".abc").on({
   mouseenter : function() {
      var id = this.id;
       //or just
      SomeFunction(this.id);
   }
});

jQuery 1.7 の時点で、.on() メソッドは、イベント ハンドラーをドキュメントにアタッチするための推奨メソッドです。以前のバージョンでは、.bind() メソッドを使用して、イベント ハンドラーを要素に直接アタッチしていました。

于 2012-05-14T18:38:17.647 に答える
1

関数に渡す必要はありません。簡単な方法があります。

$(".abc").bind({
    mouseenter : SomeFunction /* This might be a new way for you. 
                                 But its very useful sometimes */
});

function SomeFunction()
{
    var id = $(this).attr("id"); /* You can access $(this) in this function. 
                                    Here the $(this) will refer to the bound 
                                    element (i.e. of class ".abc") 
                                 */
}

単純!

于 2012-05-14T18:40:05.850 に答える
1
$('.abc').bind('mouseenter', function() {
    SomeFunction(this.id);
});
于 2012-05-14T18:39:21.777 に答える
1
$(".abc").bind('mouseenter', function() {
    var id = $(this).attr('id');
});
于 2012-05-14T18:38:37.993 に答える