2010-02-10 164 views
0

我有一些div的一些子元素。內部父div我有一個超鏈接。 如果我點擊超鏈接,那麼我想關閉當前點擊鏈接的父div,而不是其他div。關閉父div div

例:

<div class="1"><a href="#" class="closeThis">close</a></div> 
<div class="2"><a href="#" class="closeThis">close</a></div> 
<div class="3"><a href="#" class="closeThis">close</a></div> 
<div class="4"><a href="#" class="closeThis">close</a></div> 

如果我點擊類=「1」的話,我只是想隱藏類的當前父=「1」格,而不是其他的父母DIV中的鏈接。

幫我解決。 在此先感謝。

回答

3

試試這個:

您的鏈接:

<div class="1"><a href="#" class="closeThis" onclick="removeThis(this);">close</a></div> 
<div class="2"><a href="#" class="closeThis" onclick="removeThis(this);">close</a></div> 
<div class="3"><a href="#" class="closeThis" onclick="removeThis(this);">close</a></div> 
<div class="4"><a href="#" class="closeThis" onclick="removeThis(this);">close</a></div> 

<script> 
    function removeThis(field) 
    { 
    $(field).parent().hide(); 
    } 
</script> 

注:你也可以如上圖所示使用remove()代替hide()。由於

+0

嗨, 感謝您的答覆...有一些問題。該守則還關閉了其他重新設立的分部。 我想關閉當前點擊鏈接的父div,而不是其他div。鏈接的類名對於所有的Div都是一樣的。 – 2010-02-10 06:25:34

+0

@Ra:我修改了代碼,請再檢查一次,謝謝 – Sarfraz 2010-02-10 06:31:30

8

嘗試:

$("div a.closeThis").click(function(){ 
    $(this).parent().hide(); 
}); 
+0

如果他在div內的其他地方有鏈接,該怎麼辦?它將會消失! – Sarfraz 2010-02-10 06:51:23

+0

@達門 - 我建議'$('a.closeThis')'而不是'($「div a」)' – 2010-02-10 06:59:46

+0

哦,是的,你是對的,夥計們 – Darmen 2010-02-10 07:45:32