使用jquery获取HTML表列id

get html table column id with jquery

本文关键字:表列 id HTML 获取 jquery 使用      更新时间:2023-09-26

我有一个1行1列的html表,像这样:

<table id="backlog">
  <colgroup> <col width="200"></colgroup>
  <tbody>
    <tr><td id="91" tabindex="0" class="mark">BackLog</td></tr>
   </tbody>
</table>

,我想用jquery获得列id。我怎么能这么做?我试过了:

colId = $("#backlog td:first").attr('id');

如果发布的代码出现undefined错误,很可能是在运行jQuery函数时DOM还没有加载。确保在执行jQuery之前等待DOM加载完成:

$(document).ready() {
    colId = $("#backlog td:first").attr('id');
}

或者,也可以选择jQuery快捷方式:

(function($){
    colId = $("#backlog td:first").attr('id');
}(jQuery));

假设只有一行

var colId = $("#backlog td").attr("id");

Try

$(function () {
    colId = $("#backlog td:first").attr('id');
});