【问题标题】:Jquery How to skip to next iterationJquery如何跳到下一个迭代
【发布时间】:2021-05-11 05:46:43
【问题描述】:

我有一个表格,需要将<span> 附加到 td:first-child 单元格和 td:first-child 单元格。

问题:

  1. 在第一个 td 中已经存在 <span>。如果<span> 已经存在,如何跳过附加? (如果 if 语句不知何故不起作用)

  2. 我想知道为什么我的代码没有在 td:last-child 中附加 <span>

  3. 请看第一列的第二行和第三行,插入文本在单元格中的文本之后(Jill 2 first-col,Jill 2 first-col)。如何将插入文本移到前面?

请帮帮我。谢谢!

$(document).ready(function() {
  $('tr td:first-child').append('<span class="first-col"> first-col</span>');
  if ($('tr td:first-child span').length) {
    return false
  }
  $('tr td:last-child').append('<span class="second-col"> second-col</span>')
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
  </tr>
  <tr>
    <td><span class="first-col"> first-col</span>Jill</td>
    <td>Smith</td>
  </tr>
  <tr>
    <td>Jill 2</td>
    <td>Smith 3</td>
  </tr>
  <tr>
    <td>Jill 3</td>
    <td>Smith 3</td>
  </tr>

</table>

【问题讨论】:

  • td:first-child cell 和 td:first-child cell 这是什么意思?第二个first-child应该是last-child吗?
  • 嗨 Barmar,如果我使用 nth-child(2) 代替 last-child,它可以工作。
  • if ($('tr td:first-child span').length) 总是正确的......你期待什么
  • if ($('tr td:first-child span').length),我希望不再在该单元格中附加&lt;span&gt;,因为它已经有&lt;span&gt;

标签: html jquery


【解决方案1】:

您需要将if 语句放在添加新跨度的代码周围。由于您将它放在之后,因此条件始终为真,因此您返回并且永远不会到达将跨度添加到最后一个孩子的代码。

您还需要使用.each(),这样您一次只测试一个TD,而不是全部。

相反,您可以使用 :not(:has(span)) 从选择器中排除已经有跨度​​的 TD。

$(document).ready(function() {
  $('tr td:first-child:not(:has(span))').append('<span class="first-col"> first-col</span>');
  $('tr td:last-child').append('<span class="second-col"> second-col</span>')
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
  </tr>
  <tr>
    <td><span class="first-col"> first-col</span>Jill</td>
    <td>Smith</td>
  </tr>
  <tr>
    <td>Jill 2</td>
    <td>Smith 3</td>
  </tr>
  <tr>
    <td>Jill 3</td>
    <td>Smith 3</td>
  </tr>

</table>

【讨论】:

  • 感谢 Barmar 的帮助!它按预期工作。
【解决方案2】:

您必须使用each() 方法将当前tr td:first-child 定义为this,使用find() 方法和其中指定的span 标签。

$("tr td:first-child").each(function () {
    if (!$(this).find("span").length) {
        $(this).append('<span class="first-col"> first-col</span>');
    }
});

插入带有first-col 类的span 标记,将其放在条件中,并使用布尔运算符NOT

$(document).ready(function () {
    $("tr td:first-child").each(function () {
        if (!$(this).find("span").length) {
            $(this).append('<span class="first-col"> first-col</span>');
        }
    });
    $("tr td:last-child").append('<span class="second-col"> second-col</span>');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table style="width: 100%;">
    <tr>
        <th>Firstname</th>
        <th>Lastname</th>
    </tr>
    <tr>
        <td><span class="first-col"> first-col</span>Jill</td>
        <td>Smith</td>
    </tr>
    <tr>
        <td>Jill 2</td>
        <td>Smith 3</td>
    </tr>
    <tr>
        <td>Jill 3</td>
        <td>Smith 3</td>
    </tr>
</table>

【讨论】:

  • 嗨 s.kuznetsov,非常感谢您的分享!这按预期工作。我想知道不是使用if (!$(this).find("span").length) 作为您的代码,而是尝试使用if (!$(this).is('span')) if (!$(this).has('span')),这些都行不通。有什么想法吗?
  • @abcidd,嗨。方法.has 非常适合我。您忘记指定 length 属性。 -!$(this).has("span").length)。请参阅链接 - api.jquery.com/has。从逻辑上讲,在给定任务中使用哪种方法没有区别。
  • @abcidd,我忘了写is()。此方法检查当前元素,但不检查子元素。为了定义&lt;span&gt; 标签,您需要添加.children()。这就是它应该如何工作 - !$(this).children().is("span").
猜你喜欢
  • 2010-10-03
  • 2018-10-11
  • 1970-01-01
  • 2015-11-11
  • 2014-01-07
  • 2011-09-10
  • 1970-01-01
  • 2021-09-10
  • 1970-01-01
相关资源
最近更新 更多