如何使用 jQuery 选择兄弟元素?

你能帮我处理这个 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'));
}
});

基本上,我想用。属于与。每个循环中的倒计时:

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

然后把这个应用到那个按钮上:

$(button here).addClass("disabled");
$(button here).attr("disabled", "");
151830 次浏览

Since $(this) refers to .countdown you can use $(this).next() or $(this).next('button') more specifically.

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

Use jQuery .siblings() to select the matching sibling.

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

Try -

   $(this).siblings(".bidbutton").addClass("disabled").attr("disabled", "");
$("selector").nextAll();
$("selector").prev();

you can also find an element using Jquery selector

$("h2").siblings('table').find('tr');
$("h2").siblings().css({"color": "blue"});

If you want to select a specific sibling:

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

where 'index' is the index of the specific sibling within the parent container.

also if you need to select a sibling with a name rather than the class, you could use the following

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

you can select a sibling element using jQuery

 <script type="text/javascript">
$(document).ready(function(){
$("selector").siblings().addClass("classname");
});
</script>

Demo here

you can use
$(this).siblings(".bidbutton").addClass("disabled");
$(this).siblings(".bidbutton").attr("disabled","");

If I understood that correctly you're already in a loop (each) so you would always want to select that one sibling button inside each loop runthrough? Since siblings() returns an array, this would be the way to go:

$(this).siblings('.bidbutton')[0]

You can apply both things you wanted in a single line doing this:

$(this).siblings('.bidbutton')[0].addClass("disabled").attr("disabled", "");