0

I've read the documentation over and over and I can't get why this doesn't work:

From inside a function, calling the following:

alert($(this).parent().parent().html());

returns something looking like this:

<div class="something1">
    <div class="whereThisStarted">stuff</div>
    </div>
<div class="something2">stuff</div>
<div class="somethingSpecial">stuff</div>
<div class="something4">stuff</div>

I want to get "somethingSpecial". It would seem to me that either of the following should work but they both return null.

alert($(this).parent().parent().children(".somethingSpecial").html());
alert($(this).parent().parent().filter("div.somethingSpecial").html());

What's wrong with this?

Thanks

4

3 に答える 3

6

TStamper が示した方法ではなく、本当に希望する方法で行う必要がある場合は、これを試してください。

alert($(this).parent().parent().find("div.somethingSpecial").html());
于 2009-05-07T20:26:24.683 に答える
2

最も近い関数を試してみることをお勧めします-それは、あなたがやろうとしていることの線に沿っているように思えます:

jquery doc - 最も近い()

このコードは動作するはずです:

alert($(this).closest(".somethingSpecial").html());

または、$(this) が「startHere」div 内にある場合:

alert($(this).parent().closest(".somethingSpecial").html());
于 2009-05-07T20:29:59.730 に答える
0

children("div.somethingSpecial") だけでは機能しない理由はわかりませんが、これも機能することに気付きました:

alert($(this).parent().parent().children().filter("div.charactersLeft").html());
于 2009-05-07T20:30:51.880 に答える