9

<li>特定のアイテムをクリックすると、親を取得しようとしてい<li>ます。

http://jsfiddle.net/doonot/GjbZk/

をクリックするとsubmodule 1、クリックされた ID を取得できます。この場合$(this).attr('id'); 、親の ID を取得するにはどうすればよいですか?<li>module 1

私のツリーは非常に大きいので、柔軟でなければならないことに注意してください。

どうもありがとう、あなたの答えに感謝します。

4

3 に答える 3

27

このメソッドを使用してclosest、セレクターに一致する最初の祖先を取得できます。

var li = $(this).closest("li");

jQueryドキュメントから:

現在の要素から始まり、DOMツリーを上に向かって、セレクターに一致する最初の要素を取得します。

于 2012-05-04T08:23:42.333 に答える
10
var parentModule = $('this')             //the selector of your span
    .parents('li:eq(1)')                 //get the next li parent, not the direct li
    .children('.rightclickarea:first')   //get the first element, which would be the span
    .attr('id')                          //get the span id
于 2012-05-04T08:30:16.170 に答える
5
var parent = $(this).closest("li");
于 2012-05-04T08:23:53.490 に答える