下一个<a> tag in table</a>

时间:2015-03-15 16:38:20

标签: javascript jquery html

我有一张看起来像这样的表:

<table>
    <tbody>
        <tr>
            <td>
                <a id="firstTAG" class="jp-play-me" data="foo"></a>
            </td>
        </tr>
        <tr>
            <td>
                <a id="secondTAG" class="jp-play-me" data="bar"></a>
            </td>
        </tr>
        <tr>
            <td>
                <a id="thirdTAG" class="jp-play-me" data="foobar"></a>
            </td>
        </tr>
    </tbody>
</table>

一个javascript / jQuery函数:

function getnextmedia(current) {
    var res = current.split("/");
    rowId = "#" + res[5];

    $(".jp-play-me").each(function() {
        if (this.id == rowId) {
            alert(this.data);
            alert($(this).next(".jp-play-me").attr("data"));
        }
    });
}

rowid是我在表中寻找的id。我使用课程jp-play-me查找所有<a>代码,然后比较id以获得良好的<a>代码。这很好。

下一步是在表格中获取以下<a>标记,以便该函数可以读取data属性。

例如,如果rowId等于secondTAG,那么我需要获取值foobar,这是以下data <a>的值标签属性。

我尝试使用next()方法,但我猜我做错了。

如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

它不起作用,因为.next()方法返回紧跟在兄弟元素之后。由于锚元素不是兄弟,因此不会返回任何内容。

为了在下一个tr元素中获取锚元素,您需要选择最接近的tr然后在下一个{{中找到锚元素1}}:

tr
$(this).closest('tr').next().find('.jp-play-me').attr("data");

参考文献: