Jquery: Get next element from an element
How can I get the target from the with td rowspan=4
Not sure abo开发者_JAVA技巧ut:
$("#fromTd").parent("tr").next().eq($("#fromTd").attr("rowspan")-1)
<table>
<tr>...</tr>
<tr>...</tr>
<tr><td id="fromTd" rowspan="4"></td></tr>
<tr>...</tr>
<tr>...</tr>
<tr>...</tr>
<tr>...</tr> -> target (can be aleatory position..)
</table>
Correct me if I am wrong but it sounds like what you want is to get the next <tr>
after X rows where X is the rowspan of a given <td>
- 1. In other words, you want the next row into which that <td>
will NOT extend.
If that is the case, this should do the trick:
var eq = $("#fromTd").attr("rowspan") - 1;
var row = $("#fromTd").parent("tr").nextAll(':eq(' + eq + ')');
Here's a live demo: http://jsfiddle.net/wLPA9/
If you're trying to do what that line looks like it's trying to do—selecting the row after the end of the current cell's rowspan
—you would just need nextAll()
instead of next()
, which only ever returns the immediate next element sibling.
var td= $('#fromTd');
var nextr= td.parent().nextAll().eq(td.attr('rowspan')-1);
Alternatively if you've got lots of following rows and you don't want to have to select them all to pick a single one out, you could do it slightly more efficiently with the standard DOM rows
and rowIndex
properties:
var nextr= td.closest('table')[0].rows[td[0].parentNode.rowIndex+td[0].rowSpan];
精彩评论