【发布时间】:2017-04-10 06:10:54
【问题描述】:
我想在现有表中添加带有一些额外计算的额外行。该表没有标题标签,它只是弄清楚自己要应用什么 CSS。我通过下面的代码添加了一个额外的标题。现在它添加了两次标题。表的(已编辑)代码如下所示:
<!--This code is in a <form> with the id="train_form", hence the usage in the jQuery code-->
<table class="vis" style="width: 100%">
<tbody>
<tr>
<th style="width: 20%">Eenheid</th>
<th style="min-width: 400px">Behoefte</th>
<th>In het dorp/Totaal</th>
<th style="width: 120px">Rekruteren</th>
<th style="width: 80px">To Do:</th>
</tr>
<tr class="row_a">
<td class="nowrap">
<a href="#" class="unit_link" data-unit="spear">
<img src="image.png" style="vertical-align: middle" alt="" class="">
Speervechter
</a>
</td>
</tr>
<!-- There are 3 more entries here, but to not be too long I've removed them. They are not really necessary-->
<tr>
<td colspan="3">
</td>
<td>
<input class="btn btn-recruit" style="float: inherit" type="submit" value="Rekruteren" tabindex="11">
</td>
<th style="width: 80px">To Do:</th>
</tr>
</tbody>
</table>
<th style="width: 80px">To Do:</th> 行是由我的脚本添加的。问题是它还将它添加到最后一个<td>。我已经看过很多“解决方案”,但它们没有帮助。它仍然添加了两次代码(见下面的截图)。
我用来添加行的代码:
$(function() {
var done = false;
if (!done) {
$("#train_form > .vis > tbody > tr:not(.row_a, .row_b)").one().append("<th style='width: 80px'>To Do:</th>");
done = true;
};
}).one();
如您所见,我尝试使用 .one() 方法,并且尝试使用布尔值。两者都不起作用,因为这段代码仍然给出了上图中的表格。
需要说明的是,我无法控制源代码,这是一个在线浏览器游戏的脚本。
我做错了什么?
【问题讨论】:
-
api.jquery.com/one 我对您尝试使用
one()感到困惑。你在看什么 api 让你认为它可以这样使用? -
好吧,基本上只是在闲逛并继续尝试。我已经查看了您提到的 api 页面上给出的示例。除此之外,我还使用了以下来源:stackoverflow.com/questions/34476696/…、stackoverflow.com/questions/24206953/jquery-append-only-once & stackoverflow.com/questions/24076422/…
-
@Taplar,我怀疑他追的是
first(),而不是one()。 -
first()当然可以。请参考该 api 以了解one()的实际作用。
标签: javascript jquery