我有一个有很多段落的网站。其中一些有文字和图像:
<p>first paragraph</p>
<p>second paragraph</p>
<p>third paragraph</p>
<p>fourth paragraph<img alt="alt" src="src"/></p>
<p>fifth paragraph</p>
我想<img>
用新段落中的标签对段落进行编号:
<p id="1">first paragraph</p>
<p id="2">second paragraph</p>
<p id="3">third paragraph</p>
<p id="4">fourth paragraph</p>
<p id="5"><img alt="alt" src="src"/></p>
<p id="6">fifth paragraph</p>
请注意,如果第 4 段中的文字在图片之后,那么最终的情况应该是:
<p id="1">first paragraph</p>
<p id="2">second paragraph</p>
<p id="3">third paragraph</p>
<p id="4"><img alt="alt" src="src"/></p>
<p id="5">fourth paragraph</p>
<p id="6">fifth paragraph</p>
现在我只有这段代码,但它没有将图像添加到单独的段落中:
elem.find('p').each(function () {
id++;
$(this).attr('id', 'id_' + id);
});