我的问题是:我如何遍历标签以便我可以对表格行进行条带化?请记住:如果解决方案在 stripeRows() 函数中,则必须对其进行抽象(不引用标记名称、类名等)。该函数对表格的行进行条带化处理,如果您查看 JavaScript 代码,我认为这将是显而易见的。
大部分都完成了(我认为),但我在文件的一个方面遇到了问题。解决方案必须在不编辑 HTML 或 CSS、没有 jQuery 和没有 innerHTML 的情况下抽象和解决。
我是否需要遍历 stripeMeTable() 和 webcoursesTable() 函数或 stripeRows(tableID,odd,even,over) 函数中的行?
我在下面包括了我的 HTML 和 JavaScript。
我需要 JavaScript 文件来处理这两个 HTML 页面。
好的,这是代码。谢谢你的帮助。
HTML 代码(第 1 页):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Striped Tables</title>
<link rel="stylesheet" type="text/css" href="striped-tables.css" />
<script type="text/javascript" src="striped-tables.js"></script>
</head>
<body>
<h1>Striped Tables</h1>
<table id="stripeme">
<tr>
<th>Food</th>
<th>Rating</th>
</tr>
<tr class="lines">
<td>Chocolate</td>
<td>delicious</td>
</tr>
<tr>
<td>Shrimp</td>
<td>delicious</td>
</tr>
<tr class="lines">
<td>Brussel sprouts</td>
<td>vile</td>
</tr>
<tr>
<td>Raspberry</td>
<td>delicious</td>
</tr>
<tr class="lines">
<td>Tofu</td>
<td>not even a real food</td>
</tr>
<tr>
<td>Pizza</td>
<td>delicious</td>
</tr>
<tr class="lines">
<td>Honey</td>
<td>never spoils</td>
</tr>
</table>
</body>
</html>
HTML(第 2 页):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Is your script abstracted?</title>
<link rel="stylesheet" type="text/css" href="abstraction.css" />
<script type="text/javascript" src="striped-tables.js"></script>
</head>
<body>
<h1>Another Striped Table</h1>
<table id="webcourses">
<tr>
<th>Course number</th>
<th>Course content</th>
</tr>
<tr class="core_course">
<td>WEB-140</td>
<td>design principles</td>
</tr>
<tr class="core_course">
<td>WEB-115</td>
<td>XHTML and beginning CSS</td>
</tr>
<tr class="core_course">
<td>WEB-210</td>
<td>advanced CSS</td>
</tr>
<tr>
<td>WEB-215</td>
<td>JavaScript</td>
</tr>
<tr>
<td>WEB-182</td>
<td>PHP</td>
</tr>
<tr>
<td>WEB-250</td>
<td>integration of MySQL and PHP</td>
</tr>
</table>
</body>
</html>
JavaScript 文件:
addLoadEvent(stripemeTable);
addLoadEvent(webcoursesTable);
function stripemeTable() {
// get the <tr> tags:
var stripemeRows = document.getElementsByTagName('tr');
// IF the tableID is NOT stripeme, exit the stripemeTable function:
if (!document.getElementById || !document.getElementsByTagName || !document.getElementById('stripeme')) {
return false;
// ELSE loop through the rows and use the stripeRows function:
} else {
for (var i = 0; i < stripemeRows.length; i++) {
stripeRows('stripeme','pri-stripe','sec-stripe','over-stripe');
} // end of the rows FOR loop
} // end of the tableID test IF/ELSE
} // end of the stripemeTable function
function webcoursesTable() {
// get the <tr> tags
var webcoursesRows = document.getElementsByTagName('tr');
// IF the tableID is NOT webcourses, exit the webcoursesTable function:
if (!document.getElementById || !document.getElementsByTagName || !document.getElementById('webcourses')) {
return false;
// ELSE loop through the rows and use the stripeRows function:
} else {
for (var i = 0; i < webcoursesRows.length; i++) {
stripeRows('stripeme','pri-stripe','sec-stripe','over-stripe');
} // end of the rows FOR loop
} // end of the tableID test IF/ELSE
} // end of the webcoursesTable function
function stripeRows(tableID,odd,even,over) {
// get a table with a tableID
var table = document.getElementById(tableID);
// loop through the table rows:
for (var i = 0; i < rows.length; i++) {
// get the current className of the table rows:
var oldClassName = rows[i].className;
// append the even className to the oldClassName:
var even = rows[i].oldClassName;
even += " " + even;
// append the over className to the oldClassName:
var over = rows[i].oldClassName;
over += " " + over;
// IF the table row is the first row, do not stripe that row:
if (rows[0]) {
addClass(rows[0],oldClassName);
// ELSE IF the table rows are odd, stripe the rows with the odd class:
} else if (rows[i]/2 != 0 && !rows[0]) {
addClass(rows[i],odd);
// ELSE the table rows are even, stripe the rows with the even class:
} else {
addClass(rows[i],even);
} // end of the first/even/odd rows IF
// handle the onmouseover event for the table rows:
mouseOver = rows[i].onmouseover;
mouseOver = function() {
addClass(rows[i],over);
} // end of the onmouseover anonymous function
// handle the onmouseout event for the table rows:
mouseOut = rows[i].onmouseout;
mouseOut = function() {
addClass(rows[i],oldClassName);
} // end of the onmouseout anonymous function
} // end of the table rows FOR loop
} // end of the stripeRows function
function addClass(element,theClass) {
// IF the table row does not have a className, append the required className:
if (!element.className) {
element.className = theClass;
// ELSE append the required className to the className(s) that are already assigned to that row:
} else {
var newClassName = element.className;
newClassName += " ";
newClassName += theClass;
element.className = newClassName;
} // end of the className IF/ELSE
} // end of the addClass function
function addLoadEvent(func) {
// assign the window.onload function to a variable:
var oldonload = window.onload;
// IF the window.onload does not call a function:
if (typeof window.onload != 'function') {
window.onload = func;
// ELSE set the window.load:
} else {
window.onload = function() {
oldonload();
func();
} // end of the oldonload anonymous function
} // end of the typeof IF
} // end of the addLoadEvent function
我认为是问题所在的行(请记住,函数需要保持原样,我不能在 stripeRows() 函数中引用标签名称)。问题是:如何在 stripeRows() 函数中以一般抽象的方式遍历行:
function stripeRows(tableID,odd,even,over) {
// get a table with a tableID
var table = document.getElementById(tableID);
// get the table rows:
var rows = tableID.rows;
// loop through the table rows:
for (var i = 0; i < rows.length; i++) {