如何使用 Javascript 或 Jquery 检索 <a> 标签内的 table header 数据?

How to retrieve table header data within <a> tag using Javascript or Jquery?

如何使用 jQuery 或 JavaScript 检索 <a> 标签(例如 Name 中的 table header 数据?

我的 html 是:

<table style="width:100%">
    <tr>
        <th><a href="www.a.com">Name<a></th>
        <th colspan="2">Telephone</th>
    </tr>
    <tr>
        <td>Bill Gates</td>
        <td>555 77 854</td>
        <td>555 77 855</td>
    </tr>
</table>

我会给你的 link 标签和 ID,然后使用:

document.getElementByID(linkID).href;

我不确定我是否答对了你的问题,但下面的方法不应该起作用吗?

$('th').find('a').each(function(){console.log($(this).text())});

-> 你的结果 Name

<table style="width:100%">
    <tr>
        <th><a href="www.a.com">Name<a></th>
        <th><a href="www.a1.com">Name1<a></th>
        <th><a href="www.a2.com">Name2<a></th>
        <th><a href="www.a3.com">Name3<a></th>
        <th colspan="2">Telephone</th>
    </tr>
    <tr>
        <td>Bill Gates</td>
        <td>555 77 854</td>
        <td>555 77 855</td>
    </tr>
</table>

-> 结果是:

Name
Name1
Name2
Name3

使用 Jquery 您可以使用选择器

$('table tr th a').html()

Working Fiddle

您可以使用 $("table tr th a").text();$("table tr th a").html();