Select nth child in jquery javascript

后端 未结 2 938
盖世英雄少女心
盖世英雄少女心 2021-01-26 11:45

select nth child like this I want to select seconds child .

$(this).prev().children[1].removeClass(\"necry\").addClass(\"necry_er\");

And this

相关标签:
2条回答
  • 2021-01-26 12:06

    what about something like this?

    var nec = $(this).parent().find(".necry");
    nec.removeClass("necry");
    nec.addClass("necry_er");
    
    0 讨论(0)
  • 2021-01-26 12:29

    Use eq() to reduce a set of matched elements to the one at the specified index.

    $(this).prev().children().eq(1).removeClass("necry").addClass("necry_er");
    

    There's also a :nth-child selector:

    $('#elementID:nth-child(2)').doSomething();
    

    To just swap the two classes you can do:

    $('.necry').toggleClass('necry necry_er');
    

    How exactly to go about finding the element you want is a little hard to tell, as there is no explanation as to what this is or what context it is in ?

    0 讨论(0)
提交回复
热议问题