如何使用 jQuery 选择同级元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7463242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:53:20  来源:igfitidea点击:

How do I select a sibling element using jQuery?

jqueryselector

提问by Only Bolivian Here

Can you help me with this jQuery selector?

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

Basically, I want to select the element with .bidbutton class that belongs in the same parent as the .countdown in the each loop:

基本上,我想选择具有 .bidbutton 类的元素,该类与每个循环中的 .countdown 属于同一父级:

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

And then apply this to that button:

然后将其应用于该按钮:

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

回答by Madara's Ghost

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

使用 jQuery.siblings()选择匹配的兄弟。

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

回答by JohnD

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

回答by Bharath Kumaar

Here is a link which is useful to learn about select a siblings element in Jquery.

这是一个链接,可用于了解如何在 Jquery 中选择兄弟元素。

How do I select a sibling element using jQuery

如何使用 jQuery 选择同级元素

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

you can also find an element using Jquery selector

您还可以使用 Jquery 选择器查找元素

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

For more information, refer this link next(), nextAll(), prev(), prevAll(), find() and siblings in JQuery

有关更多信息,请参阅此链接next()、nextAll()、prev()、prevAll()、find() 和 JQuery 中的兄弟

回答by Sourav Basak

$("h2").siblings().css({"color": "blue"});

Details are described in the source below:

详细信息在以下来源中描述:

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

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

回答by Bhavesh

jQuery provides a method called "siblings()"which helps us to return all sibling elements of the selected element. For example, if you want to apply CSS to the sibling selectors, you can use this method. Below is an example which illustrates this. You can try this example and play with it to learn how it works.

jQuery 提供了一个名为“siblings()”的方法,它帮助我们返回被选元素的所有兄弟元素。例如,如果要将 CSS 应用于兄弟选择器,则可以使用此方法。下面是一个示例,说明了这一点。你可以试试这个例子并使用它来了解它是如何工作的。

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

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

回答by Peter Meadley

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.

其中 'index' 是父容器内特定兄弟的索引。

回答by Shobi

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]');

回答by Developer

you can select a sibling element using jQuery

您可以使用 jQuery 选择同级元素

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

Demo here

演示在这里

回答by AlienWebguy

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

由于$(this)是指.countdown可以使用$(this).next()$(this).next('button')更具体。

回答by ipr101

Try -

尝试 -

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