2011-04-15 55 views
0

我必须在元素('.fav')的jQuery集中单击元素的父节点。 有没有人有任何建议,我该怎么做?jQuery - 排除被单击元素的父节点

$('.fav .favFrame').click(function(){ 
    $('.fav').fadeOut(400);   //exclude the .fav that child .favFrame was clicked here 
}); 

THX

+0

你的意思是,让类“fav”的所有元素都淡出,而元素是已被点击的类“favFrame”的元素的父元素? – Hannes 2011-04-15 13:18:27

回答

2

使用.not()

$('.fav .favFrame').click(function(){ 
    $('.fav').not($(this).parent()).fadeOut(400); //exclude the .fav that child .favFrame was clicked here 
}); 
2

试试这个:

$('.fav .favFrame').click(function(){ 
    var notThis = $('.fav').not($(this).parent()); 
    notThis.fadeOut(400); //fade all except this parent 
}); 

小提琴:http://jsfiddle.net/maniator/djx2M/

+1

'.not()'采用参数 – bpierre 2011-04-15 13:23:16

1

试试这个(这个作品即使.fav的不是favFrame的直接父):

$('.fav .favFrame').click(function(){  
    $('.fav').not($(this).closest(".fav")).fadeOut(400); 
}); 
+0

难道你不能用同样的方法使用''(this).parents('。fav')'? – Neal 2011-04-15 13:27:05

+0

@Neal:是的,你可以...... – Chandu 2011-04-15 13:28:01

1
$('.fav .favFrame').click(function() { 
    var myParent = $(this).closest('.fav'); 
    $('.fav').not(myParent).fadeOut(400); 
}); 

这样,你不想淡出没有得到在所有受影响的元素。