Javascript 使用jQuery获取表中的列id

Javascript 使用jQuery获取表中的列id,javascript,jquery,html-table,Javascript,Jquery,Html Table,我有这个桌子结构 <table id = "table1"> <thead> <tr id = "header_row"> <th> <a id = "a1">ABC</a> </th> <th> <a id = "a2">DEF</a> </th> <th> <a id =

我有这个桌子结构

<table id = "table1">
   <thead>
      <tr id = "header_row">
         <th> <a id = "a1">ABC</a> </th>
         <th> <a id = "a2">DEF</a> </th>
         <th> <a id = "a3">GHI</a> </th>
         <th> <a id = "a4">JKL</a> </th>
      </tr>
   </thead>
   <tr>.....data filling up the table.....</tr>
   <tr>.............</tr>
   .
   .
</table>

基础知识
DEF
GHI
JKL
…填写表格的数据。。。。。
.............
.
.
因此,从这个表中,我想获取值“a2”,你能告诉我怎么做吗?谢谢


编辑:页面上还有许多其他的
标记,因此如果有任何方法可以使用id提取?

您可以通过此
$(“#a2”).text()获取

你可以通过这个
$(“#a2”).text()获得

因此,我想从这个表中获取值“a2”

您的意思是获取标题行中第二列的id

试试这个

$("#header_row th a:eq(1)").attr("id")
如果您只是想根据其
id
获取已知
a
ABC,那么

$("#a2").html(); //or text()
因此,我想从这个表中获取值“a2”

您的意思是获取标题行中第二列的id

试试这个

$("#header_row th a:eq(1)").attr("id")
如果您只是想根据其
id
获取已知
a
ABC,那么

$("#a2").html(); //or text()

要获取
#a2
的值,只需执行以下操作:

纯JavaScript:

document.getElementById('a2').innerHtml;
jQuery:

$('#a2').text();

旁注。这句话:

因此,我想从这个表中获取值“a2”

让我觉得您在同一页的不同表中使用了相同的
id
s,并且希望从特定表中检索数据,不是吗?
这种情况是错误的,<代码> ID >代码> s>强>必须在整个页面中是唯一的,所以如果是你的例子,考虑使用类

来获取<代码>αA2<代码>的值。 纯JavaScript:

document.getElementById('a2').innerHtml;
jQuery:

$('#a2').text();

旁注。这句话:

因此,我想从这个表中获取值“a2”

让我觉得您在同一页的不同表中使用了相同的
id
s,并且希望从特定表中检索数据,不是吗?
这种情况是错误的,<代码> ID >代码> s>强>必须在整个页面中是唯一的,所以如果是你的例子,考虑使用类< /p> <代码> $(“a”)。(点击),函数(e){EngfEnguleDebug();警报(这个ID);@代码> @ RayOndabe,但这会与页面中的其他<代码> <代码>标签冲突,你能不点击一下就完成吗?那只是一个提示<代码>$('table#table1 a')。在('click')上,函数(e){e.preventDefault();alert(this.id);})
将更加具体
$('a')。在('click',函数(e){e.preventDefault();alert(this.id);})
@RayonDabre,但这会与页面中的其他
标记冲突,您能在没有
onclick
的情况下执行吗?这只是一个提示<代码>$('table#table1 a')。在('click')上,函数(e){e.preventDefault();alert(this.id);})
将更加具体!