【问题标题】:Get the id of the table row with a partcular class获取具有特定类的表行的 id
【发布时间】:2013-06-02 01:04:59
【问题描述】:

我正在尝试获取具有已知类名的表行的 ID。我正在使用 ajax 获取数据

...

 $.get("getall.php", function(data){       
    {
     $('.tableholder').append(data);
     $('tr:first-child').addClass("current");

然后给第一个孩子当前的类并且按预期工作。但是这返回未定义

var idcell  = $('table').find('tr.current').attr('id');
     alert(idcell);

为什么返回未定义?

【问题讨论】:

  • 你确定它有ID吗?您是否使用 chrome 工具检查过?
  • 我刚查了一下,有点奇怪。如果我添加像 $('tr:first-child').next().addClass("current"); 这样的类,它就可以工作。但这会将类添加到表格的第二行。
  • 你可以试试$('.tableholder').append(data); $('.tableholder tr:first-child').addClass("current");然后var idcell = $('.tableholder tr.current').attr('id'); alert(idcell);一次..

标签: jquery


【解决方案1】:

我已经确定了问题。而不是做

$('tr:first-child').addClass("current");

我做到了

$('tr:first-child').next().addClass("current");

它现在可以工作,但当前类应用于第二行。

编辑

为了将当前类添加到我使用的第一行

$('tr:eq(1)').addClass("current");

我发现这很有用https://stackoverflow.com/a/839212/1867062

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多