将TD的内容移到同一表的另一个TD中

时间:2019-01-10 17:23:23

标签: jquery append

我有许多表,并且每个表我都需要将td.risultato的内容移至td.squadre(它们在同一行中)。

我正在使用它,但是不起作用

$('table.pronostico td.risultato span').each(function() {
  $(this).appendTo('table.pronostico td.squadre');
});

有帮助吗?

使用代码更新

<table class="pronostico">  
<tr>
    <td class="squadre">some stuff here</td>
    <td class="risultato"><span>2</span></td>
</tr></table>

跨度应在td.squadre内部移动

1 个答案:

答案 0 :(得分:0)

这对我有用

$('table.pronostico td.risultato span').each(function() {
  $(this).closest("tr").find(".squadre").append($(this)); // use $(this).clone(1) to copy the span
});
.squadre { background-color:red }
.risultato { background-color:green }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="pronostico">
  <tr>
    <td class="squadre">Original content. </td>
    <td class="risultato">Move the span: <span>hello 1</span></td>
  </tr>
  <tr>
    <td class="squadre">Original content. </td>
    <td class="risultato">Move the span: <span>hello 2</span></td>
  </tr>
  <tr>
    <td class="squadre">Original content. </td>
    <td class="risultato">Move the span: <span>hello 3</span></td>
  </tr>
</table>
<hr/>
<table class="pronostico">
  <tr>
    <td class="squadre">Original content. </td>
    <td class="risultato">Move the span: <span>hello 1</span></td>
  </tr>
  <tr>
    <td class="squadre">Original content. </td>
    <td class="risultato">Move the span: <span>hello 2</span></td>
  </tr>
</table>
<hr/>
<table class="pronostico">
  <tr>
    <td class="squadre">Original content. </td>
    <td class="risultato">Move the span: <span>hello 1</span></td>
  </tr>
</table>

使用appendTo

$('table.pronostico td.risultato span').each(function() {
  $(this).appendTo(
    $('.squadre',$(this).closest("tr"))
  )
});
.squadre { background-color:red }
.risultato { background-color:green }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="pronostico">
  <tr>
    <td class="squadre">Original content. </td>
    <td class="risultato">Move the span: <span>hello 1</span></td>
  </tr>
  <tr>
    <td class="squadre">Original content. </td>
    <td class="risultato">Move the span: <span>hello 2</span></td>
  </tr>
  <tr>
    <td class="squadre">Original content. </td>
    <td class="risultato">Move the span: <span>hello 3</span></td>
  </tr>
</table>
<hr/>
<table class="pronostico">
  <tr>
    <td class="squadre">Original content. </td>
    <td class="risultato">Move the span: <span>hello 1</span></td>
  </tr>
  <tr>
    <td class="squadre">Original content. </td>
    <td class="risultato">Move the span: <span>hello 2</span></td>
  </tr>
</table>
<hr/>
<table class="pronostico">
  <tr>
    <td class="squadre">Original content. </td>
    <td class="risultato">Move the span: <span>hello 1</span></td>
  </tr>
</table>