替换 HTML dom 对象中的表


Replace Table in HTML Dom Object

是否可以选择具有特定bgcolor的表并将其替换为新表?

$message是我的DOM对象,其中包含一个具有下表的html时事通讯。

<table width="100%" bgcolor="#EEEEEE">
  some rows and columns
</table>

我希望将其替换为 $new_table,其中

$new_table = '<table width="100%" bgcolor="#EEEEEE">
                new rows and columns
             </table>';

你可以向表添加一个类,然后使用 jQuery 选择器清空表并添加新列:

<style>
.backGroundGray{
    background-color: #EEEEEE;
}
 </style>
<table width="100%"  class="backGroundGray">
    <tr>
        <td>
          some rows and columns
        </td>
    </tr>
</table>

<script>
jQuery(document).ready(function(){
     //Using jQuery
     var new_table = '<table width="100%" class="backGroundGray"><tr><td>new rows and columns</td></tr></table>';
    $('table.backGroundGray').empty().html(new_table);
});
</script>

下面是一个 JSFiddle:http://jsfiddle.net/sbritton/7Zd9f/