$ .each函数获取所有跨度数据

我正在尝试从这个html部分获取所有数据名称属性

5 Product 1 225,99 1
$  225,99
4 Product 2 699,80 1
$  699,80
Total925,79
Shipping -
Total Price
-

我正在使用这个javascript

 $(".get-all").each(function(index, element){ $('[data-name]').each(function(index, element) { if($(this).attr("data-name")) { if (startsWith($(this).attr("data-name"),"itemAmount")) { var a = $(this).html() var b = a.replace(".", ""); var c = b.replace(",", "."); params[$(this).attr("data-name") + (index+1)] = c; } else { params[$(this).attr("data-name") + (index+1)] = $(this).html(); } } }); }); 

但是我只得到第一个数据名称span atributes,就像这样

 ItemId = 5 ItemDescription = Product 1 ItemAmount 225,99 ItemQuantity = 1 

如何获取此跨度内的所有属性名称? 感谢所有回复。

一个简单的

 $('[data-name]').each(function(index, elem) { // do things with index and elem $(elem); // Will be your span }); 

将为您提供具有data-name属性的所有元素。

您可以使用$.map$.get ,您可以直接捕获具有data-name跨度,从而避免额外的if条件。

 params = $(element).find("span[data-name]").map(function() { return $(this).data("name"); }).get(); 

根据你的评论,我想你想要,(但不知道为什么)

 params = $(element).find("span[data-name]").map(function() { var rtn = $(this).data("name"); return rtn.replace((rtn == 'itemAmount') ? "." : ","), ""); }).get();