我有一个HTML格式的表,需要特别使用jQuery对其进行修改。我需要应用'header'类到主要行和‘偶数’和‘奇数’类到其他行的连续顺序。
因此,该表将在每一个连续行中有一个蓝色的主标题和浅灰色的背景行。我怎样才能做到这一点呢?我已经有了CSS,只需要弄清楚如何抓取这些行并添加类。您可以在附加的.js文件中看到我的尝试。我做错了什么?
null
"use strict";
// this JS file will call the plugin when the page loads
$(document).ready(function() {
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
null
null
"use strict";
// this is where jQuery goes
$(function(){
$('#important tr:first-child').addClass('header');
$("#important tr:not(header)").each(function(index) {
$(this).addClass((index % 2) ? "even" : "odd");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Alternating Row Plugin</title>
<link rel="stylesheet" href="altrow.css">
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="jquery.altrow.js"></script>
<script src="altrow.js"></script>
</head>
<body>
<main>
<h1>Important People in Computer Science</h1>
<table id="important">
<tr>
<th>First Name</th><th>Last Name</th><th>Date of Birth</th><th>Accomplishment</th>
</tr>
<tr>
<td>Charles</td><td>Babbage</td><td>12/26/1791</td>
<td>Originated the concept of a programmable computer, invented the first mechanical computer.</td>
</tr>
<tr>
<td>Ada</td><td>Lovelace</td><td>12/10/1815</td>
<td>First computer programmer. Wrote an algorithm for Babbage's mechanical computer.</td>
</tr>
<tr>
<td>Alan</td><td>Turing</td><td>6/23/1912</td>
<td>Invented the Turing Machine, a hypothetical device that's a model of a general purpose computer.</td>
</tr>
<tr>
<td>Grace</td><td>Hopper</td><td>12/9/1906</td>
<td>Invented the first compiler for a computer programming language, popularized the term "debugging" for fixing computer glitches.</td>
</tr>
</table>
</main>
</body>
</html>
null
正如所指出的,您不需要Javascript,您可以用CSS解决这一点。但是,由于您的问题特别要求使用Javascript,所以我将为您提供JS解决方案。首先,让我们修正一下你的错别字:
$('#important tr:first-child').addClass('header');
注意上面的大写c
。
就在上面的线下面,做类似这样的事情
$("#important tr:not(header)").each(function(index) {
$(this).addClass((index % 2) ? "even" : "odd");
});
完整解决方案:
null
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Alternating Row Plugin</title>
<link rel="stylesheet" href="altrow.css">
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="jquery.altrow.js"></script>
<script src="altrow.js"></script>
<style>
.even {
background-color: lightgray;
}
.odd {
background-color: darkgray;
}
.header {
background-color: lightblue;
}
</style>
<script>
$(function() {
$('#important tr:first-child').addClass('header');
$("#important tr:not(.header)").each(function(index) {
$(this).addClass((index % 2) ? "even" : "odd");
});
});
</script>
</head>
<body>
<main>
<h1>Important People in Computer Science</h1>
<table id="important">
<tr>
<th>First Name</th><th>Last Name</th><th>Date of Birth</th><th>Accomplishment</th>
</tr>
<tr>
<td>Charles</td><td>Babbage</td><td>12/26/1791</td>
<td>Originated the concept of a programmable computer, invented the first mechanical computer.</td>
</tr>
<tr>
<td>Ada</td><td>Lovelace</td><td>12/10/1815</td>
<td>First computer programmer. Wrote an algorithm for Babbage's mechanical computer.</td>
</tr>
<tr>
<td>Alan</td><td>Turing</td><td>6/23/1912</td>
<td>Invented the Turing Machine, a hypothetical device that's a model of a general purpose computer.</td>
</tr>
<tr>
<td>Grace</td><td>Hopper</td><td>12/9/1906</td>
<td>Invented the first compiler for a computer programming language, popularized the term "debugging" for fixing computer glitches.</td>
</tr>
</table>
</main>
</body>
</html>
对于这个简单的任务,您不需要Javascript,您可以通过CSS将even
或odd
元素作为目标:
null
tbody tr:nth-child(even) {
background-color: pink;
}
tbody tr:nth-child(odd) {
background-color: green;
color: white;
}
<main>
<h1>Important People in Computer Science</h1>
<table id="important">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Date of Birth</th>
<th>Accomplishment</th>
</tr>
<thead>
<tbody>
<tr>
<td>Charles</td>
<td>Babbage</td>
<td>12/26/1791</td>
<td>Originated the concept of a programmable computer, invented the first mechanical computer.</td>
</tr>
<tr>
<td>Ada</td>
<td>Lovelace</td>
<td>12/10/1815</td>
<td>First computer programmer. Wrote an algorithm for Babbage's mechanical computer.</td>
</tr>
<tr>
<td>Alan</td>
<td>Turing</td>
<td>6/23/1912</td>
<td>Invented the Turing Machine, a hypothetical device that's a model of a general purpose computer.</td>
</tr>
<tr>
<td>Grace</td>
<td>Hopper</td>
<td>12/9/1906</td>
<td>Invented the first compiler for a computer programming language, popularized the term "debugging" for fixing computer glitches.</td>
</tr>
<tbody>
</table>
</main>