我有一个TABLE
结构,我想使用 jQuery 函数将其元素转换为DIV
网页示例:
<table>
<tr>
<td>
<table>
<tbody>
<tr>
<td>1000+</td><td> </td><td>$0.115</td>
</tr>
<tr>
<td>2000+</td><td> </td><td>$0.106</td>
</tr>
<tr>
<td>5000+</td><td> </td><td>$0.099</td>
</tr>
<tr>
<td>10000+</td><td> </td><td>$0.092</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>Some Data</td>
</tr>
</table>
我只想替换的结构是那些在单元格 (TD
) 标签内的表格。所以制作了一个代码来过滤和替换那些驻留在 TD
标签内的表
$('table td:has(table)').replaceWith(function() {
//Replace function starts here
})
问题是我想将所有元素(如<TABLE>
,<TBODY>
,<TH>
,<TR>
和<TD>
替换为<DIV>
或<SPAN>
元素。
我怎样才能做到这一点?
<table width="95%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><div>
<div>
<div>
<div>1000+</div><div> </div><div>$0.115</div>
</div>
<div>
<div>2000+</div><div> </div><div>$0.106</div>
</div>
<div>
<div>5000+</div><div> </div><div>$0.099</div>
</div>
<div>
<div>10000+</div><div> </div><div>$0.092</div>
</div>
<div>
</div></td>
</tr>
<tr>
<td>Some Data</td>
</tr>
</table>
您可以使用
以下内容:
$("#me").click(function(){
var innerhtml = $("table tr td").html();
var innerhtml = innerhtml.replace(/table/g, "div");
var innerhtml = innerhtml.replace(/tbody/g, "div");
var innerhtml = innerhtml.replace(/tr/g, "div");
var innerhtml = innerhtml.replace(/td/g, "div");
$("table tr td").html(innerhtml);
});
它从表格内部抓取 HTML,并将 "table"、"tbody"、"tr" 和 "td" 的所有实例替换为 "div"。
这里的工作示例