2011-06-26 36 views
0
<div class="apple"> 
    <div class="abc"> 
     <input id="go"> 
    </div> 
</div> 


$("#go").click(function(){ 
    $(this).parent('.apple').hide(); // this doesn't work. 
    $(this).parent().parent().hide(); //this works 
}); 

我想讓.parent('.apple')工作。怎麼來.parent()不工作?

回答

5

jQuery.parent() function文檔:

[...]的。家長()和.parent()方法是相似的,不同之處在於後者僅在DOM樹上傳送單個級別。 [...]

換句話說,使用jQuery.parents()而不是jQuery.parent()

+0

我正要發佈這個答案,但你打敗了我。你應該補充一點,'.parent(「。selector」)'和'.parent()。filter(「。selector」)'是一樣的。 – Na7coldwater

1

輸入的父母是您的div與類abc。這就是爲什麼它不起作用。你想用父母複數上去父鏈:

$( 「#去」)點擊(函數(){

$(this).parents('.apple').hide(); 

});

請參閱此鏈接的詳細信息:http://jqueryminute.com/jquery-parent-vs-parents/

+0

我希望JQuery上升到每個父級,並找到與「apple」 – TIMEX

+0

相匹配的父級,您希望使用「父母」複數,查看我的更新。 – brendan

相關問題