2011-07-27 8 views
3

は、私は、次のコードを持っていません。リンクからH1タグを削除すると、警告呼び出しは正しく 'acitem'を返します。 H1タグを使用して、私も試したことがあります:jqueryのは()次の要素

$(this).nextUntil('.acitem') 

これでもまだ「未定義」が返されます。

JQueryでnext()関数がどのように動作するのかちょっと混乱します。リンクからH1タグを削除した場合にのみ、次の機能が正しく機能する理由を知っていますか?私が間違っていることはありますか?

ありがとうございます!兄弟のため

答えて

11

next()作品は、あなたの構造では、<a>は一人っ子<h1>であり、その技術的に<a>として何も返さないだろうnext()<h1>の唯一の息子である呼び出します。あなたがドキュメントを引用<h1>

を削除する場合はそれゆえ、なぜそれが動作します: Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector.

http://api.jquery.com/next/

nextUntil()は、ドキュメントを引用し、要素までのセレクタから始まるが、引数として指定されたセレクタを除くのセットを取得します:クエリについてhttp://api.jquery.com/nextUntil/

Get all following siblings of each element up to but not including the element matched by the selector, DOM node, or jQuery object passed.

、あなたnext()に電話する前に.parent()に電話して、親の兄弟を取得していることを確認してください。彼の叔父)

<script> 
    $(document).ready(function() { 
     $('a.link').click(function() { 
      var element = $(this).parent().next(); 

      alert(element.attr('class')); 
     }); 

    }); 
</script> 
+0

1良い答え、完全なソリューションを使用しています。 –

+1

+1叔父を使用するための+1 – ansiart