只从最后一个类获取data-id?

时间:2018-05-31 13:20:25

标签: jquery

我想总是得到最后一个数据ID。 即使我在dinamically中添加更多带有更多data-id的类。

如何获取最后一个data-id值而不是第一个?

    $( document ).ready(function() {

$(window).scroll(function() {
        if ($(window).scrollTop() >= $(document).height() - $(window).height() - 200){

    var variable = $('.report').data("id");
    alert(variable);

       }
    });

    });

// how to get data from the last one only?

<li class="report" data-id="602532">Report inappropriate</li>
<li class="report" data-id="602530">Report inappropriate</li>
<li class="report" data-id="602529">Report inappropriate</li>
<li class="report" data-id="602531">Report inappropriate</li>
<li class="report" data-id="602575">Report inappropriate</li>
<li class="report" data-id="602557">Report inappropriate</li>
<li class="report" data-id="602574">Report inappropriate</li>

https://jsfiddle.net/7m50Lyx1/

1 个答案:

答案 0 :(得分:0)

尝试:

$( document ).ready(function() {

var variable = $('.report').last("id");
alert(variable);

});