【问题标题】:jQuery only execute .append oncejQuery 只执行 .append 一次
【发布时间】: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>

&lt;th style="width: 80px"&gt;To Do:&lt;/th&gt; 行是由我的脚本添加的。问题是它还将它添加到最后一个&lt;td&gt;。我已经看过很多“解决方案”,但它们没有帮助。它仍然添加了两次代码(见下面的截图)。

我用来添加行的代码:

$(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() 方法,并且尝试使用布尔值。两者都不起作用,因为这段代码仍然给出了上图中的表格。

需要说明的是,我无法控制源代码,这是一个在线浏览器游戏的脚本。

我做错了什么?

【问题讨论】:

标签: javascript jquery


【解决方案1】:

我想你想要$.first() 而不是$.one()

$("#train_form > .vis > thead > tr:not(.row_a, .row_b)")
    .first()
    .append("<th style='width: 80px'>To Do:</th>");

【讨论】:

  • 正如我所说:“为了清楚起见,我无法控制源代码,这是一个在线浏览器游戏的脚本。”不幸的是,这并不容易......否则我现在已经做到了;)
  • 啊,开枪。很抱歉我没有仔细阅读。我会再考虑一下,如果我可以修改我的答案,我会删除它。
  • 是的!而已!它现在可以正常工作。太感谢了!有这么多的电话,不记得他们全部:p 案件结束。感谢您的帮助!
  • 太棒了!我很高兴我能够在阅读失败后赎回自己。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多