2011-09-18 25 views
31

このjQueryセレクタを手伝ってもらえますか?jQueryを使用して兄弟要素を選択するにはどうすればよいですか?

<div class="auctiondivleftcontainer"> 
    <p class="countdown">0</p> 
    <button class="btn primary bidbutton">Lance</button>        
</div> 

そして、そのボタンにこれを適用します:

$(".auctiondiv .auctiondivleftcontainer .countdown").each(function() { 
    var newValue = parseInt($(this).text(), 10) - 1; 
    $(this).text(newValue); 

    if (newValue == 0) { 
     $(this).parent().fadeOut(); 
     chat.verify($(this).parent().parent().attr('id')); 
    } 
}); 

基本的に、私は、各ループで.countdownと同じ親に属する.bidbuttonクラスを持つ要素を選択したい

$(this)以来
$(button here).addClass("disabled"); 
$(button here).attr("disabled", ""); 

答えて

65

jQuery .siblings()を使用して、一致する兄弟を選択します。

$(this).siblings('.bidbutton'); 
+2

'Siblings()'は、セレクタに一致する要素の* all *の配列を返します。 – LukeP

0

は、あなたがより具体的に$(this).next()または$(this).next('button')を使用することができます.countdownを指します。

+0

そして、彼のすぐ隣にいないとどうなりますか?それが彼の後ろにあればどうなりますか?より一般的な兄弟の解決法を使用してください。 –

+2

驚いたことに、それは彼の提供されたDOMにあり、必要がなければすべての兄弟をトラバースする理由はありません。 – AlienWebguy

4
$(this).siblings(".bidbutton") 
0

TRY -

$(this).siblings(".bidbutton").addClass("disabled").attr("disabled", ""); 
1

jQueryのは、私たちが選択した要素のすべての兄弟要素を返すことができます"siblings()"と呼ばれる方法を提供します。たとえば、兄弟セレクタにCSSを適用する場合は、このメソッドを使用できます。以下はこれを示す例です。この例題を試してみて、それを使ってそれがどのように動作するかを知ることができます。

$("p").siblings("h4").css({"color": "red", "border": "2px solid red"});

0

あなたが特定の兄弟を選択する場合:

var $sibling = $(this).siblings('.bidbutton')[index]; 

をどこ「インデックスが」親コンテナ内の特定の兄弟の指標です。

関連する問題