接下来 <a> 标记

Next <a> tag in table

本文关键字:标记 接下来      更新时间:2023-09-26

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

<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> tag 属性的值。

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

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

它不起作用,因为 .next() 方法返回紧随其后的同级元素。由于锚元素不是同级元素,因此不会返回任何内容。

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

$(this).closest('tr').next().find('.jp-play-me').attr("data");
$(".jp-play-me").each(function() {
    if (this.id == rowId) {
        // Next <a>
        $(this).closest('tr').next().find('.jp-play-me').attr("data");
    }
});

引用:

  • .next()
  • .closest()
  • .find()