首先,FIDDLE .
这是我项目的一部分,HTML 内容太多,所以我只展示结构:
<div class="line">
<table>
<tbody>
<tr>
<td>
<font face="Arial">
<p>
<!--content-->
<br>
<!--content-->
<br>
<!--content-->
<br>
<!--content-->
</p>
</font>
</td>
</tr>
</tbody>
</table>
</div>
这部分是我应该处理的内容。我应该做的是根据 <br>
将该表拆分为多个表,并保留原来的样式。也就是说,每个段落在不同的表中应该是独立的。
下面的代码显示了我之前对那些最初用<p>
分割内容的解决方案。 。所以你应该已经知道我的意图了。
function split_func($div) {
var $rows = $div.find("table tr td p"); // fail to detect tr in this demo
n = $rows.length;
$rows.each(function(i) {
var $clone = $div.clone(true, true).insertAfter($div);
$clone.find("table tr td p").each(function(j) {
if(i !== (n-j-1)) $(this).remove();
});
});
$div.remove();
}
带有注释的行是我应该组织提取函数的地方。你能告诉我如何解决这个问题吗?谢谢!
最佳答案
这将让您将每个段落拆分到自己的表格中:
$('.line p').each(function(){
var paragraphs = $(this).html().split('<br>');
for(var i=0;i<paragraphs.length;i++){
if(paragraphs[i]!= '') {
$(this).closest('table').clone().appendTo($(this).closest('.line')).find('p').html(paragraphs[i]);
}
}
});
$('.line table:first').remove();
<强> jsFiddle here
关于javascript - 从表格中的段落中提取行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17563543/