我正在尝试使用 Javascript 的 getAttribute 来获取商品价格。我是个大菜鸟,所以我只使用了 getAttribute 并将<h2>
类更改为商品价格。不过,这只获取了第一个项目的价格,我怎样才能让它获取所有的价格并将它们存储为一个数组以进行排序或其他什么?
这是我的一些 HTML:
<div class="col-sm-4">
<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">
<img src="images\site_images\bag3.jpg" alt="" height="249" />
<h2 class="881.10">$881.10</h2>
<h5>Authentic New Gucci ($1690) Micro-GG "Vernice" Crossbody w/Strap #309617, NWT</h5>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to Cart</a>
</div>
<div class="col-sm-4">
<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">
<img src="images\site_images\bag4.jpg" alt="" height="249" />
<h2 class="569.05">$569.05</h2>
<h5>AUTHENTIC NWT ($819) Gucci GG Large Brown Denim Tassell Tote #336660, w/Gft Rcpt</h5>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to Cart</a>
</div>
<div class="col-sm-4">
<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">
<img src="images\site_images\bag5.jpg" alt="" height="249" />
<h2 class="559.00">$559.00</h2>
<h5>Authentic Gucci GG Micro-Guccissima Leather Tote #309613 w/Gft Rcpt,NWT</h5>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to Cart</a>
</div>
和
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
这是我用来获取商品价格的 Javascript 代码:
function myFunction() {
var x = document.getElementsByTagName("H2")[0].getAttribute("class");
document.getElementById("demo").innerHTML = x;
}
正如你所看到的,我是一个巨大的新手,所以如果可以的话,请尽量让你的答案不那么复杂。:) 谢谢!