1

テーブルの最後の行のデータ属性を取得するにはどうすればよいですか?

<tbody id="tableNotifications">
    <tr class="notificationsRowEntry success" data-notification-timestamp="1384105387"></tr>
    <tr class="notificationsRowEntry success" data-notification-timestamp="1384105367"></tr>
    <tr class="notificationsRowEntry success" data-notification-timestamp="1384105357"></tr>
    <tr class="notificationsRowEntry success" data-notification-timestamp="1384105323"></tr>
</tbody>

問題は、データ属性を取得できず、次を使用して最後のテーブル行のコンテンツのみを取得できることです:

$data = $("#tableNotifications").last().data("notification-timestamp");

愚かな間違いをしているのかもしれませんが、それは私を夢中にさせています.

ありがとうございました。

PS私はいくつかの一意のIDを追加してからデータを取得できることを知っています

 $data = $(uniqueid).data("notification-timestamp");

しかし、私はそれを避けたいです。

4

3 に答える 3

3

これは、「tableNotifications」の ID を持つ最後の要素を見つけることです。tr最後の行を取得するには、セレクターに追加するだけです。

$data = $("#tableNotifications tr").last().data("notification-timestamp");
于 2013-11-10T19:58:18.290 に答える
3

自体trではなく、の data 属性の値を取得する必要があります。tbody

$("#tableNotifications > tr:last-child").data("notification-timestamp");
于 2013-11-10T19:50:48.097 に答える
3

使用attr()機能:

var data = $("#tableNotifications tr:last").attr("data-notification-timestamp");

セレクターは id を持つ要素#tableNotifications tr:lastの子を選択します。last trtableNotifications

また、これも機能します:

var data = $("#tableNotifications tr").last().attr("data-notification-timestamp");

明確にするために:

HTML から属性値を取得したいだけの場合は、 を使用します.attr("data-attr")。詳細については、 と の違いを.data()参照.attr()してください。

于 2013-11-10T19:52:36.270 に答える