$ .each函数获取所有跨度数据 [英] $.each function to get all spans data

查看:60
本文介绍了$ .each函数获取所有跨度数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get all data-name atributes from this html section

<div class='get-all'>
    <div class='left-wrapper'>
        <div class='field-wrapper'>
            <div class='field'> <span class='product-id' data-name='itemId'>5</span>

                <img src='carousel/images/120231671_1GG.jpg' width='52' height='52'> <span class='final-descript' data-name='itemDescription'>Product 1</span>
 <span class='product-id' data-name='itemAmount'>225,99</span>
 <span class='product-id' data-name='itemQuantity'>1</span> 
            </div>
            <div class='fix'></div>
            <div class='field'> <span class='review-price'>$ &nbsp;225,99</span>

            </div>
        </div>
        <div class='field-wrapper'>
            <div class='field'> <span class='product-id' data-name='itemId'>4</span>

                <img src='carousel/images/120231671_1GG.jpg' width='52' height='52'> <span class='final-descript' data-name='itemDescription'>Product 2</span>
 <span class='product-id' data-name='itemAmount'>699,80</span>
 <span class='product-id' data-name='itemQuantity'>1</span> 
            </div>
            <div class='fix'></div>
            <div class='field'> <span class='review-price'>$ &nbsp;699,80</span>

            </div>
        </div>
    </div>
    <div class='left-wrapper'> <span class='f-itens'>Total</span><span id='fff' class='f-value'>925,79</span>

        <hr class='f-hr' /> <span class='f-itens'>Shipping</span><span class='f-value' id='f-value'> - </span>

        <hr class='f-hr' />
        <div class='f-itens tots'>Total Price</div><span class='f-value' id='pagar'>-</span>

    </div>
</div>

我正在使用此javascript

I'm using this 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();
                }
            }
        });
    });

但是我只能得到第一个数据名称跨度属性,像这样

But I'm only get the first data-name span atributes, like this

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

如何在此范围内获取所有属性名称?感谢您的所有答复.

How To get all attribute-name inside this spans? Thanks for all replyes.

推荐答案

简单

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

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

will get you all elements with a data-name attribute.

这篇关于$ .each函数获取所有跨度数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆