1
<div class="span_group">
<span class="sp_main"><span>Sample text</span></span>
<span class="sp_main"><span>Sample text</span></span>
<span class="sp_main"><span>Sample text</span></span>
<span class="sp_main"><span>Sample text</span></span>
<span class="sp_main"><span>Sample text</span></span>
</div>
$(".span_group .sp_main span").each(function(index){
$(this).attr("id","span"+$(this).children("span").length);
});
蔭嘗試添加ID =「跨度+長」到 預期輸出的孩子跨度是像下面的jQuery基於元素長度元素添加ID
<span class="sp_main"><span id="span1">Sample text</span></span>
<span class="sp_main"><span id="span2">Sample text</span></span>
<span class="sp_main"><span id="span3">Sample text</span></span>...
請幫
那你的錯誤是什麼? – Varun