查找具有特定ID的表列的索引

时间:2019-06-06 06:14:41

标签: jquery html html-table

这是我的桌子

                 <table>
                    <thead>
                            <tr>
                                <th>0001</th>
                                <th class="hidden">0002</th>
                                <th id="myColumn">0003</th>
                                <th>0004</th>
                     ...........
                     ...........
                 </table>

我想获取ID为th的{​​{1}}的索引。结果应为myColumn
注意:我的表具有隐藏列。

3 个答案:

答案 0 :(得分:2)

您可以直接在元素上使用.index()

$('#myColumn').index()

演示

console.log($('#myColumn').index())
.hidden {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <th>0001</th>
      <th class="hidden">0002</th>
      <th id="myColumn">0003</th>
      <th>0004</th>
    </tr>
    </head>
</table>

答案 1 :(得分:0)

请检查此。我想这就是您想要的。

var index = $("#myColumn").index();
console.log("column index  " +index);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
                    <thead>
                            <tr>
                                <th>0001</th>
                                <th class="hidden">0002</th>
                                <th id="myColumn">0003</th>
                                <th>0004</th>
                                </thead>
                    
                 </table>

答案 2 :(得分:0)

在许多情况下,使用香草JS可以更轻松地实现此目的,并且更清洁。

本机DOM API将此存储在from pysnmp.hlapi import * errorIndication, errorStatus, errorIndex, varBinds = next( getCmd(SnmpEngine(), CommunityData('public'), UdpTransportTarget(('your.snmp.enabled.device.address', 161)), ContextData(), ObjectType(ObjectIdentity('YOUR-COMPANY-MIB', 'powerSystemCompany', 0)).addAsn1MibSource('file:///your/snmp/mibs/location')), ) if errorIndication: print(errorIndication) elif errorStatus: print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) else: for varBind in varBinds: print(' = '.join([x.prettyPrint() for x in varBind])) 中的每个表单元中。表格行中有HTMLTableCellElement.prototype.cellIndex(以防您担心行号):

HTMLTableRowElement.prototype.rowIndex
console.log(myColumn.cellIndex);
console.log(myColumn.parentElement.rowIndex);

相关问题