JQuery:如何选择div的第五个父元素?

时间:2014-07-07 13:01:17

标签: jquery select element parent

http://www.netindex.com/download/allcountries/

我想用德国的.ranking-item类加载div。我用.DE定位div,然后尝试获得第五个父。

var ger = $('<div />').load( "proxy.php?url=http://www.netindex.com/download/allcountries/&mode=native&callback=complete .top-20-countries .DE");

尝试:parent,.parent(),. closest()都无济于事。

1 个答案:

答案 0 :(得分:3)

HTML看起来像这样:

<ol class="ranking">
    <li class="ranking-item">
  <a class="ranking-link" href="/download/2,91/Hong-Kong/">
    <div class="figure">
      <b class="figure-content">84.01</b>
      <span class="figure-unit">Mbps </span>
    </div>
    <div class="ranking-content">
      <div class="ranking-heading">
        <i class="flag HK"></i>Hong Kong
      </div>
      <div class="ranking-chart country-list"><svg width="280" height="20">...</svg></div>
    </div>
  </a>
</li>
...
</ol>

<i>标记的第五个父级是<ol class="ranking">元素,因此使用.ranking:has(.de)作为单个选择器传入.load()

var ger = $('<div />').load( "proxy.php?url=http://www.netindex.com/download/allcountries/&mode=native&callback=complete .ranking:has(.DE)");