Javascript 同时显示两个表?

Javascript 同时显示两个表?,javascript,php,Javascript,Php,我找到了一个脚本,可以更改每一秒行的颜色,使其看起来更漂亮,但是当我在同一页上添加另一个表时,它会给第一个表上色,但第二个表没有颜色为什么 table.js的代码 jQuery(document).ready(function() { //for tables style function altRows(id){ if(document.getElementsByTagName){ var table = document.getElementB

我找到了一个脚本,可以更改每一秒行的颜色,使其看起来更漂亮,但是当我在同一页上添加另一个表时,它会给第一个表上色,但第二个表没有颜色为什么

table.js的代码

jQuery(document).ready(function() {

    //for tables style
    function altRows(id){
    if(document.getElementsByTagName){  

        var table = document.getElementById(id);  
        var rows = table.getElementsByTagName("tr"); 

        for(i = 0; i < rows.length; i++){          
            if(i % 2 == 0){
                rows[i].className = "evenrowcolor";
            }else{
                rows[i].className = "oddrowcolor";
            }      
        }
    }
}
window.onload=function(){
    altRows('alternatecolor');
}



});
jQuery(文档).ready(函数(){
//表格样式
函数altRows(id){
if(document.getElementsByTagName){
var table=document.getElementById(id);
var rows=table.getElementsByTagName(“tr”);
对于(i=0;i
php网页代码:

  <?php 
//first table
       echo'
           <table class="altrowstable" id="alternatecolor">
      <tr>
      <th>Seller</th>
      <th>price(per 1k doge)</th>
      <th>payment allowed</th>
      <th>View Trade</th>
      </tr>

      <td>Example</td>
        <td>Example</td>
          <td>Example</td>
        </tr>';



     echo'</table> <br />';  


//seccond table
 echo  '<h3>trades on going </h3>';

    echo ' <table class="gridtable">
     <table class="altrowstable" id="alternatecolor">
        <tr>
        <th>Trade User</th>
        <th>Cost($)</th>
        </tr>

     <td>Example</td>
        <td>Example</td>
          <td>Example</td>
        </tr>';

     echo'</table> ';  

?>

更好的方法是通过CSS

tr:nth-child(odd) { background-color: black, color: white }
tr:nth-child(even) { background color: white, color: black }

这将应用于页面上的所有表,而不必将HTML作为PHP或Javascript的输出处理。

更好的方法是通过CSS

tr:nth-child(odd) { background-color: black, color: white }
tr:nth-child(even) { background color: white, color: black }

这将应用于页面上的所有表,而不必处理PHP或Javascript输出的HTML。

调用
document.getElementById(id)
时,您是通过其id
alternatecolor
引用第一个表。也许在第二个表中添加一个id,然后再次调用
altRows

编辑PHP文件中的第二个表:

echo'

编辑JavaScript:

window.onload=function(){
    altRows('alternatecolor');
    altRows('secondTable');
}

调用
document.getElementById(id)
时,您正在通过其id
alternatecolor
引用第一个表。也许在第二个表中添加一个id,然后再次调用
altRows

编辑PHP文件中的第二个表:

echo'

编辑JavaScript:

window.onload=function(){
    altRows('alternatecolor');
    altRows('secondTable');
}

别凭身份证做。凭身份证做。别凭身份证做。凭身份证做。coudent figuar告诉我怎么做,你的方式帮我修好了谢谢:)coudent figuar告诉我怎么做,你的方式帮我修好了谢谢:)