使用jquery考虑colspan获取td的索引

use*_*962 0 jquery

"我希望id:Second的索引为2(考虑#First为2列).但是在$("#Second").parent().find("td").index($("#Second"))i 的帮助下得到1. Jquery中的任何Direct函数都可以执行此操作而无需使用FOR LOOPS或.each函数或任何一种循环???"

<table>
<tr>
<td colspan="2" id="First">
</td>
<td colspan="2" id="Second">
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)

T.J*_*der 7

Jquery中的任何Direct函数都可以执行此操作而无需使用FOR LOOPS或.each函数或任何类型的循环???

不,我不相信有.当然,循环是微不足道的.

var index = 0;
$("#Second").prevAll("td").each(function() {
    index += this.colSpan;
});
console.log(index);
Run Code Online (Sandbox Code Playgroud)

输出:

2

...这是该单元格的从0开始的索引,包括colspans.

实例 | 资源

同样,这个:

<table>
    <tr>
        <td>One column wide</td>
        <td colspan="2">Two columns wide</td>
        <td>One column wide</td>
        <td colspan="2" id="Last">Two columns wide</td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

该代码为我们4提供了最后一列(第5列的从0开始的索引).实例 | 资源