通过JS数组迭代,排序价格?

时间:2015-12-26 03:27:32

标签: javascript sorting price

好吧所以我正在尝试对我的网页上的项目进行排序,我使用getElementsByClassName并将其保存为节点列表或数组我不确定,但是当我调试时它会显示h2价格,h2价格,h2价格.log 。我只想要物品价格,所以我可以对它们进行排序当我查看innerHTML它有我想要的物品价格时,我如何得到它而不是'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="price">$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="price">$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="price">$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>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

我的Javascript:

function myFunction() {
    var items = document.getElementsByClassName('price');
    var prices = [];
}

1 个答案:

答案 0 :(得分:2)

尝试使用Array.prototype.slice()Array.prototype.map()HTMLCollection转换为ArrayNumber()String.prototype.replace()RegExp {{1 },/\$/

Array.prototype.sort()