0

我无法弄清楚这一点。

我想知道如何有条件地将下表更改为 div。

<table border="0" cellpadding="0" cellspacing="0" width="100%">
 <tr>
  <td>
     Text Here
  </td>
 </tr>
</table>

有多个表。只有包含指定文本的内容才应替换为div. 而且里面的内容table应该只是放在replacement里面div

我不确定这是否可以做到。

4

1 回答 1

3

您可以使用replaceWith方法:

$('table').replaceWith(function() {
    return '<div>' + $(this).text() + '</div>';
});

http://jsfiddle.net/MHuns/

如果要选择具有特定文本内容的表格元素,可以使用filter方法:

$('table').filter(function(){
    return $.trim($(this).text()) === 'Text Here';
}).replaceWith(function () {
    return '<div>' + $(this).text() + '</div>';
});

http://jsfiddle.net/MHuns/2/

它的一个版本作为可重复使用的 jQuery 插件实现:

http://jsfiddle.net/87XhU/

于 2013-03-25T22:22:15.830 回答