5

次のように、複数のセレクターを使用してイベントを処理する場合:

$('.item a, .another-item a').click(function(e) {

});

イベントをトリガーした親セレクターを特定することはできますか? .itemそれとも?.another-item_

ありがとうございました!

4

5 に答える 5

8

セレクターはほぼ何でもかまいませんので、次のような特定のものを確認する必要があります。

if($(this).is('.item a')){
  //your code here
} else if ($(this).is('.another-item a')){
  //more here
}
于 2012-10-17T19:23:06.077 に答える
4

jQuery を使用できますis

if($(this).is('.item a')){
      // code
   }
   else if($(this).is('.another-item a')){//remove 'if ' in case there are only two selector separated by commas.
        //code..

   }
于 2012-10-17T19:25:09.507 に答える
3

これを試して

$(this).closest('.item').length > 0
于 2012-10-17T19:25:18.193 に答える
1

.itemとが2つの異なるノードに属している場合、.another-item以下のように試すことができます。

$('.item a, .another-item a').click(function(e) {
    if ($(this).closest('.item').length) {  //if it is '.item'

    } else {

    }
});
于 2012-10-17T19:26:04.883 に答える
1

確認してもいい:

var isItem = $( this ).parents( '.item:first' ).length > 0,
    isAnotherItem = $( this ).parents( '.another-item:first' ).length > 0;
于 2012-10-17T19:24:52.423 に答える