2011-04-29 45 views

回答

2

this是你的锚,你可以使用:

$(this).closest("div") 
+0

什么会这样做,如果有一个什么直接在标签下面的div?或在它上面......就像在兄弟姐妹中一样,不会这样休息吗? – samccone 2011-04-29 19:17:55

+1

@samccone:不,它不会。 http://api.jquery.com/closest/“获取匹配选择器的第一个* ancestor *元素,*从当前元素开始,并通过DOM树向前推进。” – 2011-04-29 19:23:59

0

您可以使用parent() jQuery function

$(".container a").click(function() { 
    // do something with the parent div here 
    var div = $(this).parent(); 
}) 
2

可以使用.parent();单独直接父母,或.parent('.container');搜索方式弥补一匹配

例如:

$('a').bind('click',function(){ 
    $(this).parent().css('background-color','red'); 
}); 
0

$('a').click(function(){$(this).parent().hide();});

这会抢了一个标签的父,然后隐藏它作为一个例子父()函数是你想用这个