在匹配集之后添加 TR,如果 jQuery 中匹配集为空,则在末尾添加 TR

发布于 2024-09-29 14:48:05 字数 2438 浏览 3 评论 0原文

问题

我想在表中的给定匹配集之后添加新的 TR,或者如果匹配集为空,则在表的末尾添加新的 TR。给定以下 html:

<table id="blax">
   <thead>
   </thead>
   <tbody>
      <tr class="alpha">
         <td>some stuff</td>
         <td>more stuff</td>
      </tr>
      <tr class="alpha">
         <td>some stuff</td>
         <td>more stuff</td>
      </tr>
      <!-- new "alpha" tr to go here-->
      <tr class="beta">
         <td>some stuff</td>
         <td>more stuff</td>
      </tr>
      <tr class="beta">
         <td>some stuff</td>
         <td>more stuff</td>
      </tr>
      <!-- but new "gamma" tr to go at end-->
   </tbody>'
</table>

我知道如何在某些匹配的集合之后添加新的 tr,例如:

var b = $('#blax tbody');
var newAlphaTr = $('<tr class="alpha"></tr>')
   .insertAfter(b.find('tr.alpha:last'));

但是如果为类 gamma (表中尚不存在)运行相同的代码会怎么样? ?有什么简单的方法可以使用相同的方法将新 元素的插入点放在表格的末尾用于 alpha 插入的代码

function insertAtPosition(parentElement, afterElement, newElement) {
   // some code here that does the job for either "alpha" or "gamma"
}

function creatElement(className) {
   insertAtPosition(
      b,
      b.find('tr.' + className + ':last'),
      $('<tr class="' + className + '"></tr>'
   );
}

createElement('alpha');
createElement('gamma');

我需要帮助填写“此处的一些代码”块。

现在闲聊

顺便说一句,insertAfter 返回插入的元素还是后面添加的项目?我猜测 after 返回插入的项目,而 insertAfter 返回其附加的项目,就像 append的方式一样appendTo 工作,是吗? jQuery 文档很好,但有一些大漏洞,例如没有列出每个函数返回的内容(我很困惑为什么有人会错过这一点)。事实上,当我写这个问题时,我需要集中注意力才能确保我答对了。

只是闲想,如果 appendappendTo 支持一个可选参数,该参数可以是索引位置,也可以是之后的直接子元素(或直接子元素集)所有这些项目都将被附加。对于索引,它可以是所需的位置,也可以是要插入项目的位置,因此 0 或 -1 表示作为第一个子项。

var b = $('#blax tbody');
var newAlphaTr = $('<tr class="alpha"></tr>')
   .appendTo(b, $('tr.alpha', b));
// or perhaps a selector
var newAlphaTr2 = $('<tr class="alpha"></tr>')
  .appendTo(b, 'tr.alpha:last');

这将在指定的子集之后插入一个具有类 alpha 的新 tr。我可以将 :last 添加到选择器,但这种方式会很好。如果“td.alpha”的匹配集为空,那么它将像通常的追加操作一样,并将其放在现有子项之后。

The Question

I'd like to add a new TR in a table after a given matched set, or instead, at the end of the table if the matched set is empty. Given the following html:

<table id="blax">
   <thead>
   </thead>
   <tbody>
      <tr class="alpha">
         <td>some stuff</td>
         <td>more stuff</td>
      </tr>
      <tr class="alpha">
         <td>some stuff</td>
         <td>more stuff</td>
      </tr>
      <!-- new "alpha" tr to go here-->
      <tr class="beta">
         <td>some stuff</td>
         <td>more stuff</td>
      </tr>
      <tr class="beta">
         <td>some stuff</td>
         <td>more stuff</td>
      </tr>
      <!-- but new "gamma" tr to go at end-->
   </tbody>'
</table>

I know how to add a new tr after some matched set, for example:

var b = $('#blax tbody');
var newAlphaTr = $('<tr class="alpha"></tr>')
   .insertAfter(b.find('tr.alpha:last'));

But what if the same code is run for class gamma (which doesn't exist in the table yet)? What's an easy way to then make the insertion point of the new <tr class="gamma"></tr> element just go at the end of the table using the same code as used for the alpha insertion?

function insertAtPosition(parentElement, afterElement, newElement) {
   // some code here that does the job for either "alpha" or "gamma"
}

function creatElement(className) {
   insertAtPosition(
      b,
      b.find('tr.' + className + ':last'),
      $('<tr class="' + className + '"></tr>'
   );
}

createElement('alpha');
createElement('gamma');

I need help filling in the "some code here" block.

Now for some idle chatter

And incidentally does insertAfter return the inserted element or the item it was added after? I'm guessing that after returns the inserted item and insertAfter returns the item it was attached to, like like the way append and appendTo work, yes? The jQuery docs are nice but have some big holes such as not listing what is returned from each function (and I'm quite puzzled about how anyone could miss this). In fact as I was writing this question it took some real concentration to make sure I'd gotten right.

Just idly thinking, it would be nice if append and appendTo supported an optional parameter which is either an index position or an immediate child element (or set of immediate child elements) after ALL of which the item will be appended. In the case of the index it could either be the desired position or the position after which to insert the item, so that 0 or -1 would mean as the first child.

var b = $('#blax tbody');
var newAlphaTr = $('<tr class="alpha"></tr>')
   .appendTo(b, $('tr.alpha', b));
// or perhaps a selector
var newAlphaTr2 = $('<tr class="alpha"></tr>')
  .appendTo(b, 'tr.alpha:last');

This would insert a new tr with class alpha after specified child set. I could add :last to the selector but this way would be nice. If the matched set of 'td.alpha' is empty, then it would act like append normally does and put it after the existing children.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫﹏色ふ单纯 2024-10-06 14:48:05
<tbody id='zeta'/>

$('#zeta').append('<tr class="alpha"></tr>');

你愿意把它当作 if/else 吗?

if( $('.alpha').length>0){
//追加到类上
}其他{
//附加到tbody上
}

<tbody id='zeta'/>

$('#zeta').append('<tr class="alpha"></tr>');

are you willing to do it as a if/else?

if( $('.alpha').length>0){
//append on class
}else{
//append on tbody
}

不顾 2024-10-06 14:48:05

自从提出这个问题以来,我已经有了近 2 年的 jQuery 经验,现在我可以看到答案很简单:

var b = $('#blax tbody'),
   newAlphaTr = $('<tr class="alpha"></tr>')
      .insertAfter(
         b.find('tr.alpha:last, tr:last')
            .first()
      );

不要只在 tr 上与所需的类匹配,还要匹配最后一个 < code>tr 无论类别如何。然后,使用 first() 获取第一个,这将是我们要在后面插入的那个。问题解决了。

With almost 2 more years of jQuery experience under my belt since asking the question, I can see now that the answer is easy:

var b = $('#blax tbody'),
   newAlphaTr = $('<tr class="alpha"></tr>')
      .insertAfter(
         b.find('tr.alpha:last, tr:last')
            .first()
      );

Instead of matching just on the tr with the desired class, also match the last tr regardless of class. Then, grab the first with first(), which will be the one we want to insert after. Problem solved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文