下面是我的表格。我想将所有第二个td
移到第一个td
之前。我的代码主要起作用,但它只移动td
内的内容,而不移动td
本身(内容和样式)。
$(document).ready(function() {
$('.mrQuestionTable tr').each(function() {
$(this).find('td:eq(0)').before($(this).find('td:eq(0)').next().html());
});
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table summary="" class="mrQuestionTable" style="display: inline-block;">
<tr>
<td>
<span class="mrQuestionText" style="color: #00659d;"><b><u>Chronic Inflammatory Demyelinating Polyneuropathy (CIDP)</u></b> patients personally managed in the past year</span>
</td>
<td>
<div></div>
<span style="">
<div></div>
<input type="text" name="_QA2_Qa_QA2__Scale" id="_Q0_Q0_Q0" class="mrEdit" autocomplete="off" style="width: 3em;" maxlength="2" value=""/>
</span>
</td>
</tr>
<tr>
<td>
<span class="mrQuestionText" style="color: red;"><b><u>Guillain-Barre Syndrome (GBS)</u></b> patients personally managed in the past year</span>
</td>
<td>
<div></div>
<span style="">
<div></div>
<input type="text" name="_QA2_Qb_QA2__Scale" id="_Q0_Q1_Q0" class="mrEdit" autocomplete="off" style="width: 3em;" maxlength="2" value=""/>
</span>
</td>
</tr>
</table>
&#13;
答案 0 :(得分:0)
将您的选择器更改为$(this).find('td:eq(0)').before($(this).find('td:eq(1)'));
$(document).ready(function() {
$('.mrQuestionTable tr').each(function() {
$(this).find('td:eq(0)').before($(this).find('td:eq(1)'));
});
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table summary="" class="mrQuestionTable" style="display: inline-block;">
<tr>
<td>
<span class="mrQuestionText" style="color: #00659d;"><b><u>Chronic Inflammatory Demyelinating Polyneuropathy (CIDP)</u></b> patients personally managed in the past year</span>
</td>
<td>
<div></div>
<span style="">
<div></div>
<input type="text" name="_QA2_Qa_QA2__Scale" id="_Q0_Q0_Q0" class="mrEdit" autocomplete="off" style="width: 3em;" maxlength="2" value=""/>
</span>
</td>
</tr>
<tr>
<td>
<span class="mrQuestionText" style="color: red;"><b><u>Guillain-Barre Syndrome (GBS)</u></b> patients personally managed in the past year</span>
</td>
<td>
<div></div>
<span style="">
<div></div>
<input type="text" name="_QA2_Qb_QA2__Scale" id="_Q0_Q1_Q0" class="mrEdit" autocomplete="off" style="width: 3em;" maxlength="2" value=""/>
</span>
</td>
</tr>
</table>
&#13;
答案 1 :(得分:0)
您需要从该方法中删除html()并使用类似的内容
$(document).ready(function(){
$('.mrQuestionTable tr').each(function() {
$(this).find('td:eq(0)').before($(this).find('td:eq(0)').next());
});
});