选择的jQuery JavaScript的第n个孩子 [英] Select nth child in jquery javascript

查看:228
本文介绍了选择的jQuery JavaScript的第n个孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选择n个子像这样我想选择秒的孩子。

  $(本)preV()儿童[1] .removeClass(necry)addClass(necry_er)。;

和这个HTML

 < D​​IV CLASS =reg_label>
    < D​​IV CLASS =高清>家庭与LT; / DIV>
    < D​​IV CLASS =necry>必要的实地< / DIV>
    < D​​IV CLASS =清除>&安培; NBSP;< / DIV>
< / DIV>

我希望这个结果:

 < D​​IV CLASS =necry_er>必要的实地< / DIV>


解决方案

使用 EQ()以减少匹配的元素,以一个指定索引处。

  $(本)preV()儿童()方程(1).removeClass(necry)addClass(necry_er)。;

还有一个:第n个孩子选择:

  $('#elementID:第n个孩子(2)')DoSomething的()。

要只是交换两个类,你可以做:

  $('necry。')toggleClass('necry necry_er')。

究竟是如何去寻找你想要的是一个有点分不清元素,因为没有解释什么这个是或者是在什么情况下?

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

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

And this HTML

<div class="reg_label">
    <div class="def">Family</div>
    <div class="necry">Necessary Field</div>
    <div class="clear">&nbsp;</div>
</div>

I expect this result:

<div class="necry_er">Necessary Field</div>

解决方案

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 ?

这篇关于选择的jQuery JavaScript的第n个孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆