使用jQuery获取考虑colspan的TD索引 [英] Get Index of a td considering the colspan using jquery

查看:106
本文介绍了使用jQuery获取考虑colspan的TD索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望id:Second的索引为2(将#First视为2列).但是在$("#Second").parent().find("td").index($("#Second"))的帮助下,我得到了1.Jquery中的任何Direct函数都可以做到这一点,而不必使用FOR LOOPS或.each函数或任何形式的循环???"

"I want the index of id:Second to be 2(Considering #First as 2 columns). But with the help of $("#Second").parent().find("td").index($("#Second")) i get 1. Any Direct function in Jquery to do this without i having to use FOR LOOPS or .each function or any sort of loop???"

<table>
<tr>
<td colspan="2" id="First">
</td>
<td colspan="2" id="Second">
</td>
</tr>
</table>

推荐答案

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

Any Direct function in Jquery to do this without i having to use FOR LOOPS or .each function or any sort of loop???"

不,我不相信那里.当然,循环很简单.

No, I don't believe there is. The loop is trivial, of course.

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

输出:

2

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

...which is the 0-based index of that cell, including 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>

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

That code gives us 4 for the last column (the 0-based index of the 5th column). Live Example | Source

这篇关于使用jQuery获取考虑colspan的TD索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆