HTML 結構:<li><a><span>1</span><span>2</span><span>3</span></a></li>,需將每個 <li> 下第 2、3 個 <span> 設置為指定值。
$('#ul li span:not(:first-child)').html('new value');
// 或更精確:第 2、3 個 (索引 1、2)
$('#ul li').each(function() {
$(this).find('span').eq(1).html('value2');
$(this).find('span').eq(2).html('value3');
});
:not(:first-child):排除第一個 span,匹配第 2~N 個。.eq(index):精確選擇指定索引 (0-based) 的元素。<ul id="ul">
<li><a><span>A1</span><span class="c2">--</span><span class="c3">--</span></a></li>
<li><a><span>B1</span><span class="c2">--</span><span class="c3">--</span></a></li>
</ul>
<script>
$('#ul li span:not(:first-child)').text('已設置'); // 所有非第一個
// 或分別設置
$('#ul li').each(function() {
$(this).find('span').eq(1).text('第二個');
$(this).find('span').eq(2).text('第三個');
});
</script>