我正在使用jquery来移动表行,当我释放行时,我想得到最近的TR的ID,其中包含类group_heading
当我正在移动的行被释放时,我使用以下内容获取它的ID:
var row_id = $(row).prop('id');
console.log (row_id)
会产生sprint_145975
如何使用班级sprint_145975
获取最接近group_heading
的TR的ID?
这是表格布局。
<tr id="present_sprints" class="group_heading nodrag">
<td colspan="4">LIST1</td>
</tr>
<tr id="sprint_145664" style="cursor: move;">
<td>First round - in sprint</td>
<td>2012-09-19</td>
<td>2012-09-27</td>
</tr>
<tr id="sprint_145975" class="toggler_row" style="cursor: move;">
<td>Third round</td>
<td>2012-10-04</td>
<td>2012-10-11</td>
</tr>
<tr id="sprint_145966" class="toggler_row" style="cursor: move;">
<td>Release prep</td>
<td>2012-10-20</td>
<td>2012-10-27</td>
</tr><tr id="sprint_145665" class="toggler_row" style="cursor: move;">
<td>Second round</td>
<td>2012-09-28</td>
<td>2012-10-04</td>
</tr>
<tr id="future_sprints" class="group_heading nodrag">
<td colspan="4">Future Sprints</td>
</tr>
<tr id="sprint_145964" class="toggler_row" style="cursor: move;">
<td>Fifth run</td>
<td>2012-10-27</td>
<td>2012-11-03</td>
</tr>
<tr id="sprint_145965" class="toggler_row" style="cursor: move;">
<td>Fourth round</td>
<td><input type="text" value="2012-10-13" <="" td="">
</td><td>2012-10-20</td>
</tr>
对于上面的例子,我希望得到 ID ='present_sprints'
如果移动的行的ID是sprint_145965,我希望最近的TR与类group_heading的ID为 future_sprints
我该怎么做?
我试过了:
$('#sprint_145975').closest('.group_heading).prop('id);
$('#sprint_145975').closest('tr.group_heading).prop('id);
但那不起作用..
由于
答案 0 :(得分:2)
您正在寻找.prevAll()
而不是.closest()
。您可以使用.eq()
仅获取第一个。您应该使用.attr()
代替.prop()
。
var heading = $('#sprint_145975').prevAll('.group_heading').eq(0);
console.log( heading.attr('id') );
heading = $('#sprint_145964').prevAll('.group_heading').eq(0);
console.log( heading.attr('id') );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr id="present_sprints" class="group_heading"></tr>
<tr id="sprint_145975"></tr>
<tr id="future_sprints" class="group_heading"></tr>
<tr id="sprint_145964"></tr>
</table>