0

我需要选择每个$('div span:eq(0)').text()inside $('div#first')。如何将这两个选择器串在一起?

<div id="first">Some stuff to be ignored.</div>
<div>
  <div>
    <ul>
      <li>
        <span>Some text</span> <!-- need this -->
        <span>Skipped text</span>
      </li>
    </ul>
  </div>
  <div>
    <ul>
      <li>
        <span>Some text</span>  <!-- and this -->
        <span>Skipped text</span>
      </li>
    </ul>
  </div>
  <!-- this pattern continues unknown # of times -->
</div>
4

2 回答 2

2

尝试使用li而不是div.

// Using #first.next()-v as #first just has a &nbsp;
$('li', $('#first').next()).find('span:eq(0)').text('Updated text');

演示:http: //jsfiddle.net/rFUB8/1/

于 2012-10-03T22:13:58.493 回答
0
$('div span:eq(0)', 'div#first').text()

或者,如果您想一次处理一个:

$('div span:eq(0)', 'div#first').each(function() {
  console.log($(this).text());
});

如果您正在谈论将这些跨度放在“div#first”中,您可以使用:

$('div#first').append($('div span:eq(0)'));
于 2012-10-03T22:13:12.503 回答