php
2014-04-03 63 views -1 likes 
-1

是否可以選擇具有特定bgcolor的表並將其替換爲新表?替換HTML Dom對象中的表格

$ 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>'; 
+0

我可以通過'$ old_table = $ dom-> getElementsByTagName('table') - > item(3); $ old_table-> parentNode-> removeChild($ old_table);'但不知道如何用$ new_table – AZee

回答

0

你可以添加一個類表,然後使用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/

+0

替換它是啊謝謝你的幫助,但不幸的是這並沒有加載到瀏覽器。該表位於名爲$ message的HTML DOM Object中。 – AZee

相關問題