【问题标题】:Getting the 2nd child element获取第二个子元素
【发布时间】:2011-07-23 09:14:07
【问题描述】:

我还是 jquery 的新手,我有这个代码

<div>
   abcsdf
   <div> first child</div>
   <div> second child</div>
</div>

我想得到第二个孩子,它们是使用 append 动态填充的,我不知道如何得到它。

我想显示

$('the second element inner html here').dialog() etc..

希望有人可以帮助我。

谢谢

【问题讨论】:

标签: jquery html jquery-selectors


【解决方案1】:

有多种方法可以做到这一点。我将假设顶层 div 的 id 为“top”。这可能是最好的一个:

$('#top > :nth-child(2)').whatever();

$('#top').children(':first-child').next().whatever();

或者如果你知道至少有 2 个孩子

$($('#top').children()[1]).whatever();

【讨论】:

  • what is what();,如果我正在编写 whaterver() 函数来选择一个元素,我会得到错误。
  • whatever() 是一个超级机密函数
【解决方案2】:

查看此链接

Nth child selecter

或者你也可以试试 :eq Selector

Eq selector

【讨论】:

    【解决方案3】:

    使用nth-Child Selector。 例如:$('div:nth-child(2)')

    【讨论】:

      【解决方案4】:

      也许这听起来很傻,但 $(".item").first().next() 可以解决问题。

      【讨论】:

        【解决方案5】:

        如何给 div 一个 id,然后通过 $('#mySecondDiv') 抓取它,这取决于您如何动态生成它...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-17
          • 1970-01-01
          • 2017-03-11
          • 1970-01-01
          • 2012-05-15
          • 1970-01-01
          相关资源
          最近更新 更多