次のように、複数のセレクターを使用してイベントを処理する場合:
$('.item a, .another-item a').click(function(e) {
});
イベントをトリガーした親セレクターを特定することはできますか? .item
それとも?.another-item
_
ありがとうございました!
次のように、複数のセレクターを使用してイベントを処理する場合:
$('.item a, .another-item a').click(function(e) {
});
イベントをトリガーした親セレクターを特定することはできますか? .item
それとも?.another-item
_
ありがとうございました!
セレクターはほぼ何でもかまいませんので、次のような特定のものを確認する必要があります。
if($(this).is('.item a')){
//your code here
} else if ($(this).is('.another-item a')){
//more here
}
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..
}
これを試して
$(this).closest('.item').length > 0
.item
とが2つの異なるノードに属している場合、.another-item
以下のように試すことができます。
$('.item a, .another-item a').click(function(e) {
if ($(this).closest('.item').length) { //if it is '.item'
} else {
}
});
確認してもいい:
var isItem = $( this ).parents( '.item:first' ).length > 0,
isAnotherItem = $( this ).parents( '.another-item:first' ).length > 0;