¿Cómo selecciono un elemento hermano usando jQuery?


¿Puedes ayudarme con este selector de jQuery?

$(".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'));
    }
});

Básicamente, quiero seleccionar el elemento con.clase bidbutton que pertenece al mismo padre que el .cuenta atrás en cada bucle:

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

Y luego aplicar esto a ese botón:

$(button here).addClass("disabled");
$(button here).attr("disabled", "");
Author: Tchami, 2011-09-18

8 answers

Usar jQuery .siblings() para seleccionar el hermano correspondiente.

$(this).siblings('.bidbutton');
 76
Author: Madara Uchiha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-09-18 17:52:58
$(this).siblings(".bidbutton")
 4
Author: JohnD,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-09-18 17:52:52
$("h2").siblings().css({"color": "blue"});

Los detalles se describen en la siguiente fuente:

Http://www.namasteui.com/traversing-siblings /

 2
Author: Sourav Basak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-12 13:29:48

Aquí hay un enlace que es útil para aprender acerca de seleccionar un elemento hermanos en Jquery.

¿Cómo selecciono un elemento hermano usando jQuery

$("selector").nextAll(); 
$("selector").prev(); 

También puede encontrar un elemento usando Jquery selector

$("h2").siblings('table').find('tr'); 

Para obtener más información, consulte este enlace next (), nextAll(), prev(), prevAll (), find () y hermanos en jQuery

 1
Author: Bharath Kumaar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-08 13:38:09

JQuery proporciona un método llamado "hermanos()" lo que nos ayuda a devolver todos los elementos hermanos del elemento seleccionado. Por ejemplo, si desea aplicar CSS a los selectores hermanos, puede usar este método. A continuación se muestra un ejemplo que ilustra esto. Puedes probar este ejemplo y jugar con él para aprender cómo funciona.

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

 1
Author: Bhavesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-19 11:21:56

Dado que $(this) se refiere a .countdown puede usar $(this).next() o $(this).next('button') más específicamente.

 0
Author: AlienWebguy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-09-18 17:52:50

Try -

   $(this).siblings(".bidbutton").addClass("disabled").attr("disabled", "");
 0
Author: ipr101,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-09-18 17:53:26

Si desea seleccionar un hermano específico:

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

Donde 'index' es el índice del hermano específico dentro del contenedor padre.

 0
Author: Peter Meadley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-05 12:19:11