如何获取我的最后一个类元素data-id?
HTML
我的目的是得到我的最后一个数字li data-id,但我尝试了一些代码,它确实有效。
我的代码
1。
var lastProductItem = $("#productCategory-ul li figure:last-child").attr("data-id");
2。
var lastProductItem = $("#productCategory-ul > li figure:last-child").data("id");
那么如何获得我的productCategory-ul最后的data-id?
首先纠正HTML
代替
当您打算定位最后一个li
元素时,请使用:last-child
selector。 那么你可以获得后代figure
后可以获取id
。
var lastProductItem = $("#productCategory-ul li:last-child figure").data("id");
console.log($("#productCategory-ul li:last-child figure").data("id"))
-
-
-
-
HTML
jQuery的
var lastProductItem = $("#productCategory-ul > li:last-child figure").attr("data-id"); alert(lastProductItem);
你可以玩这样的最后一项
$( "ul li:last-child" ).css({ color:"red", fontSize:"80%" }) .hover(function() { $( this ).addClass( "solast" ); }, function() { $( this ).removeClass( "solast" ); });