如何使用 jQuery 在每个表格单元格中设置增量 CSS 类?
我有一个通过数据库填充的表,它的呈现方式如下(它可以有任意数量的引用“时间”的列,5 列,8 列,2 列等):
<table id="eventInfo">
<tr>
<td class="name">John</td>
<td class="date">Dec 20</td>
<td class="**time**">2pm</td>
<td class="**time**">3pm</td>
<td class="**time**">4pm</td>
<td class="event">Birthday</td>
</tr>
<tr>
<td class="name">Billy</td>
<td class="date">Dec 19</td>
<td class="**time**">6pm</td>
<td class="**time**">7pm</td>
<td class="**time**">8pm</td>
<td class="event">Birthday</td>
</tr>
使用 jQuery,我想遍历每个表行并仅在“class='time'”的表单元格上增量设置附加类名,以便结果为:
<table id="eventInfo">
<tr>
<td class="name">John</td>
<td class="date">Dec 20</td>
<td class="**time** **timenum-1**">2pm</td>
<td class="**time** **timenum-2**">3pm</td>
<td class="**time** **timenum-3**">4pm</td>
<td class="event">Birthday</td>
</tr>
<tr>
<td class="name">Billy</td>
<td class="date">Dec 19</td>
<td class="**time** **timenum-1**">6pm</td>
<td class="**time** **timenum-2**">7pm</td>
<td class="**time** **timenum-3**">8pm</td>
<td class="event">Birthday</td>
</tr>
我只能让它计算“class ='time'”的所有表格单元格,而不是计算其自己的表格行中的每个单元格。这是我用 jQuery 尝试过的:
$(document).ready(function() {
$("table#eventInfo tr").each(function() {
var tcount = 0;
$("td.time").attr("class", function() {
return "timenum-" + tcount++;
})
//writes out the results in each TD
.each(function() {
$("span", this).html("(class = '<b>" + this.className + "</b>')");
});
});
});
不幸的是,这只会导致:
<table id="eventInfo">
<tr>
<td class="name">John</td>
<td class="date">Dec 20</td>
<td class="**time** **timenum-1**">2pm</td>
<td class="**time** **timenum-2**">3pm</td>
<td class="**time** **timenum-3**">4pm</td>
<td class="event">Birthday</td>
</tr>
<tr>
<td class="name">Billy</td>
<td class="date">Dec 19</td>
<td class="**time** **timenum-4**">6pm</td>
<td class="**time** **timenum-5**">7pm</td>
<td class="**time** **timenum-6**">8pm</td>
<td class="event">Birthday</td>
</tr>
感谢您的帮助!
I have a table populated via a DB and it renders like so (it could have any number of columns referring to "time", 5 columns, 8 columns, 2 columns, etc):
<table id="eventInfo">
<tr>
<td class="name">John</td>
<td class="date">Dec 20</td>
<td class="**time**">2pm</td>
<td class="**time**">3pm</td>
<td class="**time**">4pm</td>
<td class="event">Birthday</td>
</tr>
<tr>
<td class="name">Billy</td>
<td class="date">Dec 19</td>
<td class="**time**">6pm</td>
<td class="**time**">7pm</td>
<td class="**time**">8pm</td>
<td class="event">Birthday</td>
</tr>
With jQuery, I'd like to go through each Table Row and incrementally set an additional class-name on only the Table Cells where "class='time'" so that the result would be:
<table id="eventInfo">
<tr>
<td class="name">John</td>
<td class="date">Dec 20</td>
<td class="**time** **timenum-1**">2pm</td>
<td class="**time** **timenum-2**">3pm</td>
<td class="**time** **timenum-3**">4pm</td>
<td class="event">Birthday</td>
</tr>
<tr>
<td class="name">Billy</td>
<td class="date">Dec 19</td>
<td class="**time** **timenum-1**">6pm</td>
<td class="**time** **timenum-2**">7pm</td>
<td class="**time** **timenum-3**">8pm</td>
<td class="event">Birthday</td>
</tr>
I've only been able to get it to count all of the Table Cells where "class='time'" and not each set within its own Table Row. This is what I've tried with jQuery:
$(document).ready(function() {
$("table#eventInfo tr").each(function() {
var tcount = 0;
$("td.time").attr("class", function() {
return "timenum-" + tcount++;
})
//writes out the results in each TD
.each(function() {
$("span", this).html("(class = '<b>" + this.className + "</b>')");
});
});
});
Unfortunately, this only results in:
<table id="eventInfo">
<tr>
<td class="name">John</td>
<td class="date">Dec 20</td>
<td class="**time** **timenum-1**">2pm</td>
<td class="**time** **timenum-2**">3pm</td>
<td class="**time** **timenum-3**">4pm</td>
<td class="event">Birthday</td>
</tr>
<tr>
<td class="name">Billy</td>
<td class="date">Dec 19</td>
<td class="**time** **timenum-4**">6pm</td>
<td class="**time** **timenum-5**">7pm</td>
<td class="**time** **timenum-6**">8pm</td>
<td class="event">Birthday</td>
</tr>
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$("td.time")
选择 所有 具有time
类的表格单元格,而不仅仅是该行中的单元格。添加对该行的引用作为(第二个)上下文参数,就像您在另一个each
循环中所做的那样:$("td.time", this)
$("td.time")
select all table cells with classtime
not only those in that row. Add a reference to the row as the (second) context parameter just as you are doing in the othereach
loop:$("td.time", this)