English 中文(简体)
外交部
原标题:Alternating Table Rows With Javascript issue

我有以下文字,努力增加奇迹,甚至连课堂,以改变课桌上行刑。

function alternate(){ 
if(document.getElementsByTagName){  
var table = document.getElementsByTagName("table");
var rows = document.getElementsByTagName("tr");
    for(i = 0; i < rows.length; i++){
  //manipulate rows   
      if(i % 2 == 0){  
        rows[i].className = "even"; 
      }else{  
        rows[i].className = "odd"; 
      }
    }
  } 
}

However a problem arises when there is more than one table on a page. I need the counter to reset for each table on the page so the first row of each table always has the same class (i.e. odd). Currently the second table on the page will just carry on counting rows odd-even so it will start on a different class if the first table has an odd number of rows.

谁能帮助我改变这一法典以实现这一目标?

最佳回答

各位:

function alternate() {
    var i, j, tables, rows;

    tables = document.getElementsByTagName(  table  );

    for ( i = 0; i < tables.length; i += 1 ) {
        rows = tables[i].rows;

        for ( j = 0; j < rows.length; j += 1 ) {
            rows[j].className = j % 2 ?  even  :  odd ;
        }
    }   
}

http://jsfiddle.net/simevidas/w6rvd/“rel=“nofollow”http://jsfiddle.net/simevidas/w6rvd/


替代解决办法:

(将for的编码改为for Each insigns使代码更加贴近。) 编号:。

function alternate() {
    var tables = document.getElementsByTagName(  table  );

    [].forEach.call( tables, function ( table ) {
        [].forEach.call( table.rows, function ( row, i ) {
            row.className = i % 2 ?  even  :  odd ;
        });
    }); 
}

问题回答

页: 1

function alternate(){ 
  if(document.getElementsByTagName){  
    var table = document.getElementsByTagName("table");
    // each table
    for(a = 0; a < table.length; a++){
      var rows = table[a].getElementsByTagName("tr");
      for(i = 0; i < rows.length; i++){
        //manipulate rows   
        if(i % 2 == 0){  
          rows[i].className = "even"; 
        }else{  
          rows[i].className = "odd"; 
        }
      }
    }
  }
}

根据表格进行这项工作

 function alternate(){ 
 if(document.getElementsByTagName){  
var table = document.getElementsByTagName("table");

var rows = document.getElementsByTagName("tr");

for(var a = 0; a < table.length; a++){
{
   for(var i = 0; i < table[a].rows.length; i++){
  //manipulate rows   
  if(i % 2 == 0){  
    table[a].rows[i].className = "even"; 
     }else{  
     table[a].rows[i].className = "odd"; 
      }
    }

    }

  } }

}




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签