循环使用不同div中的值

时间:2018-03-20 15:22:12

标签: javascript arrays google-tag-manager

我试图遍历不同的div。我有产品阵列,但它们显示在不同的div中。有没有办法循环它们?我想把可用的产品(array.length)放在一个数组中。代码如下:



<div id=Googletagsholderdiv data-gtm-ecommerce.purchase.actionField.id="x"
  data-gtm-ecommerce.purchase.actionField.affiliation="x" 
  data-gtm-ecommerce.purchase.actionField.revenue="x" 
  data-gtm-ecommerce.purchase.actionField.tax="x" 
  data-gtm-ecommerce.purchase.actionField.shipping="x"
  data-gtm-ecommerce.purchase.actionField.coupon="x">
  <div data-gtm-ecommerce.purchase.products[0].name="x" 
    data-gtm-ecommerce.purchase.products[0].id="x" 
    data-gtm-ecommerce.purchase.products[0].price="x" 
    data-gtm-ecommerce.purchase.products[0].brand="x" 
    data-gtm-ecommerce.purchase.products[0].category="x"
    data-gtm-ecommerce.purchase.products[0].variant="x" 
    data-gtm-ecommerce.purchase.products[0].quantity="x" 
    data-gtm-ecommerce.purchase.products[0].coupon="x" 
    data-gtm-ecommerce.purchase.products[0].dimensionX="x">
  </div>
  <div data-gtm-ecommerce.purchase.products[1].name="x" 
    data-gtm-ecommerce.purchase.products[1].id="x" 
    data-gtm-ecommerce.purchase.products[1].price="x" 
    data-gtm-ecommerce.purchase.products[1].brand="x" 
    data-gtm-ecommerce.purchase.products[1].category="x"
    data-gtm-ecommerce.purchase.products[1].variant="x" 
    data-gtm-ecommerce.purchase.products[1].quantity="x" 
    data-gtm-ecommerce.purchase.products[1].coupon="x" 
    data-gtm-ecommerce.purchase.products[1].dimensionX="x">
  </div>
  <div data-gtm-ecommerce.purchase.products[2].name="" 
    data-gtm-ecommerce.purchase.products[2].id="x" 
    data-gtm-ecommerce.purchase.products[2].price="x" 
    data-gtm-ecommerce.purchase.products[2].brand="x" 
    data-gtm-ecommerce.purchase.products[2].category=""
    data-gtm-ecommerce.purchase.products[2].variant="x" 
    data-gtm-ecommerce.purchase.products[2].quantity="x" 
    data-gtm-ecommerce.purchase.products[2].coupon="x" 
    data-gtm-ecommerce.purchase.products[2].dimensionX="x">
  </div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

我认为你应该用你想要的数据为你的所有div提供一个类,然后用jQuery获取它们。

答案 1 :(得分:0)

您可以使用您喜欢的任何选择器选择父元素,并使用子数组获取子元素。

Yoi可以使用for..of循环遍历此数组:

var Googletagsholderdiv = document.getElementById("Googletagsholderdiv");
for (var div of Googletagsholderdiv.children) {
  console.log(div);
}
<div id=Googletagsholderdiv data-gtm-ecommerce.purchase.actionField.id="x" data-gtm-ecommerce.purchase.actionField.affiliation="x" data-gtm-ecommerce.purchase.actionField.revenue="x" data-gtm-ecommerce.purchase.actionField.tax="x" data-gtm-ecommerce.purchase.actionField.shipping="x"
  data-gtm-ecommerce.purchase.actionField.coupon="x">
  <div data-gtm-ecommerce.purchase.products[0].name="x" data-gtm-ecommerce.purchase.products[0].id="x" data-gtm-ecommerce.purchase.products[0].price="x" data-gtm-ecommerce.purchase.products[0].brand="x" data-gtm-ecommerce.purchase.products[0].category="x"
    data-gtm-ecommerce.purchase.products[0].variant="x" data-gtm-ecommerce.purchase.products[0].quantity="x" data-gtm-ecommerce.purchase.products[0].coupon="x" data-gtm-ecommerce.purchase.products[0].dimensionX="x"></div>
  <div data-gtm-ecommerce.purchase.products[1].name="x" data-gtm-ecommerce.purchase.products[1].id="x" data-gtm-ecommerce.purchase.products[1].price="x" data-gtm-ecommerce.purchase.products[1].brand="x" data-gtm-ecommerce.purchase.products[1].category="x"
    data-gtm-ecommerce.purchase.products[1].variant="x" data-gtm-ecommerce.purchase.products[1].quantity="x" data-gtm-ecommerce.purchase.products[1].coupon="x" data-gtm-ecommerce.purchase.products[1].dimensionX="x"></div>
  <div data-gtm-ecommerce.purchase.products[2].name="" data-gtm-ecommerce.purchase.products[2].id="x" data-gtm-ecommerce.purchase.products[2].price="x" data-gtm-ecommerce.purchase.products[2].brand="x" data-gtm-ecommerce.purchase.products[2].category=""
    data-gtm-ecommerce.purchase.products[2].variant="x" data-gtm-ecommerce.purchase.products[2].quantity="x" data-gtm-ecommerce.purchase.products[2].coupon="x" data-gtm-ecommerce.purchase.products[2].dimensionX="x"></div>