我有这个代码:
$(".feed-item .field-content p").each(function(i) {
$(this).addClass("item" + (i+1));
});
此代码帮助我在此标记内的段落中添加类:
<div class="feed-item">
<div class="field-content">
<p>This must be class item1</p>
<p>This must be class item2</p>
</div>
</div>
<div class="feed-item">
<div class="field-content">
<p>This must be class item1</p>
<p>This must be class item2</p>
</div>
</div>
但我需要总是从数字 1 开始计数。我解释得好吗?现在我得到这个结果:
<div class="feed-item">
<div class="field-content">
<p class="item1">This must be class item1</p>
<p class="item2">This must be class item2</p>
</div>
</div>
<div class="feed-item">
<div class="field-content">
<p class="item3">This must be class item1</p>
<p class="item4">This must be class item2</p>
</div>
</div>
这就是我需要的(注意区别在于与第二个字段内容 div 中的 p 标签关联的类):
<div class="feed-item">
<div class="field-content">
<p class="item1">This must be class item1</p>
<p class="item2">This must be class item2</p>
</div>
</div>
<div class="feed-item">
<div class="field-content">
<p class="item1">This must be class item1</p>
<p class="item2">This must be class item2</p>
</div>
</div>
我正在处理这个链接:http: //jsfiddle.net/bosspetta/RJkP5/