jQuery HTML/CSS获取所有span数据名称属性 [英] Jquery HTML/CSS Geting all spans data-name atributes
问题描述
此问题与其他此处有关. 我发现了问题的一部分,并希望有人能帮助我彻底解决它.
This question has to do with this other here. I found part of the problem and would like someone to help me to solve it altogether.
我发现实际上我的旧脚本可以工作,我只需要正确地调整它即可.看到以下情况
I discovered that in fact my old script works and i only need to adapt it correctly. See the following situation
使用此javascript
var addCartData = function(params) {
$("#content-wrapper .get-all div ").each(function(index, element){
$(element).find("span").each(function(){
if($(this).attr("data-name")) {
if (startsWith($(this).attr("data-name"),"itemAmount")) {
//rtn.replace((rtn == 'itemAmount') ? "." : ","), "");
params[$(this).attr("data-name") + (index+1)] = $(this).html().replace(($(this).html() ?',':","),".");
alert($(this).html().replace(($(this).html() ?',':","),"."));
} else {
params[$(this).attr("data-name") + (index+1)] = $(this).html();
}
}
});
});
};
我在spans元素中获得了所有数据名称[itemId,itemDescription,itemAmount,itemQuantity],因此脚本运行良好.但是,由于设计问题,我需要使脚本[或类似脚本]以相同的方式为以下html代码 [获取所有span数据名称] :
I'm getting all data-names [itemId, itemDescription, itemAmount, itemQuantity] inside spans elements, then the script is working perfectly. But, because design issues, i need to make the script [or similar] works with the same way [getting all span data-name] for the following html code:
<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'>Panfleto 80x40 500un</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'>R$ 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'>Samsung-G-Duos</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'>R$ 699,80</span>
</div>
</div></div>
<div class='left-wrapper'>
<span class='f-itens'>Valor total dos itens</span><span id='fff' class='f-value'>925,79</span>
<hr class='f-hr' />
<span class='f-itens'>Frete</span><span class='f-value' id='f-value'> - </span>
<hr class='f-hr' />
<div class='f-itens tots'>Total a pagar</div><span class='f-value' id='pagar' >-</span>
</div>
</div>
<!------------------------------------------------------------------------------------------->
问题是,对于这个新的html使用相同的脚本,我得到了几次,例如三倍,而不是一次获得$ 225.99的价值,我得到了3次.其他领域也是如此.我该如何修改脚本,以便与旧的html代码一起使用时只能获得一次日期名称?
The problem is that using the same script for this new html I get the items several times, tripled, for example, rather than get the value $ 225.99 once, I receive 3 times. So does the other fields. How can I adapt the script so you can get the date-names only once as when used with the old html code??
推荐答案
根据@ V.Salles的要求,我将自己的评论重新发布为答案,因为这导致了解决方案.
As requested by @V.Salles, I'm re-posting my comment as an answer, since it led to the solution.
当您说新html"时,我不确定您指的是什么,并且 旧的html",但由于更为具体,也许可以使用:if 您将$(#content-wrapper .get-all div").each(替换为 $(#content-wrapper .get-all").find("div.field").each(.这应该 针对每个字段集定位容器,以便内部 每个字段只浏览一次.
I'm not sure what you are referring to when you say "new html" and "old html", but maybe this will work due to being more specific: if you replace $("#content-wrapper .get-all div ").each( with $("#content-wrapper .get-all").find("div.field").each( . This should target the container for each set of fields so that the inner part goes through each field only once.
根据@ V.Salles,对我的建议进行以下调整已解决了该问题:
As per @V.Salles, the following adjustment to my suggestion has solved the problem:
$("#content-wrapper .get-all").find("div.field-wrapper").each(
这篇关于jQuery HTML/CSS获取所有span数据名称属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!