简体   繁体   English

为具有相同类的元素添加分隔符

[英]Add separator to elements with same class

This html:这个html:

<table>
<tr>
  <td>
    <a class='link-separated'>foo</a>
  </td>
</tr>
<tr>
<td>
  <a class='link-separated'>foo</a>
  <a class='link-separated'>bar</a>
</td>
</tr>
<tr>
  <td>
    <a class='link-separated'>foo</a>
    <a class='link-separated'>bar</a>
    <a class='link-separated'>xyz</a>
  </td>
</tr>
</table>

will produce:将产生:

foo
foo bar
foo bar xyz

What I want to get is:我想得到的是:

foo
foo | bar
foo | bar | xyz

I've managed something like this: https://jsfiddle.net/61yfcnjh/ but this solution is weird - .link-separated is used twice.我管理过这样的事情: https : //jsfiddle.net/61yfcnjh/但这个解决方案很奇怪 - .link-separated被使用了两次。 How can it be done better?怎样才能做得更好?

You can use this code to further simplify it:您可以使用此代码进一步简化它:

$("td a.link-separated:not(:last-child)").after(" | ");

Try it:尝试一下:

 $("td a.link-separated:not(:last-child)").after(" | ");
 <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script> <table> <tr> <td> <a href="#" class='link-separated'>foo</a> </td> </tr> <tr> <td> <a href="#" class='link-separated'>foo</a> <a href="#" class='link-separated'>bar</a> </td> </tr> <tr> <td> <a href="#" class='link-separated'>foo</a> <a href="#" class='link-separated'>bar</a> <a href="#" class='link-separated'>xyz</a> </td> </tr> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM