开发者

Shifting up TDs by classname

Each TD on my table has a class assigned to it. I would like to iterate over each TR and shift up all TDs of the same class name if the current TR d开发者_开发知识库oes not contain that particular TD. I would like to do it by shifting up tds and not by re-creating a table. For instance:

<table>
    <tr>
        <td class="c1">A</td>
        <td class="c2">A</td>
    </tr>
    <tr></tr>
    <tr>
        <td class="c1">B</td>
    </tr>
    <tr>
        <td class="c1">C</td>
        <td class="c2">B</td>
    </tr>
</table>

So I end up with something like this:

<table>
    <tr>
        <td class="c1">A</td>
        <td class="c2">A</td>
    </tr>
    <tr>
        <td class="c1">B</td>
        <td class="c2">B</td>
    </tr>
    <tr>
        <td class="c1">C</td>
    </tr>
    <tr></tr>
</table>

Any help is appreciated!


$('tr').each(function(i,elem) {
  if ($(elem).find('.c1').length == 0) {
    $('tr:gt('+i+')').each(function() {
      if ($(this).find('.c1').length) {
        elem.append($(this).find('.c1'));
        return false;
      }
    });
  }
});

Same goes for c2

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜