猿问

如何在结帐总额的末尾添加零?

我想在结帐总金额上有一个零,因为在末端以零结尾的产品上,总金额显示为这样 £1.5


那么,如何更改它以像这样显示1.50英镑而不会弄乱其他产品呢?


任何代码片段都将不胜感激。


<!DOCTYPE html>

<html>

<head>

   

</head>

<body>


<header class="main-header">

    <nav class="main-nav nav">

    </nav>

</header>


    <div class="shop-items">

        <div class="shop-item">

            <span class="shop-item-title">Album 0</span>&nbsp;

            <div class="shop-item-details">

<span class="shop-item-price">£12.50</span>

            <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>

        </div>

  

<div class="shop-item">

        <span class="shop-item-title">Album 2</span>&nbsp;

        <div class="shop-item-details">

            <span class="shop-item-price">£14.50</span>

           

            <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>

        </div>

  

  

    <div class="shop-item">

        <span class="shop-item-title">Album 4</span>&nbsp;

        <div class="shop-item-details">

<span class="shop-item-price">£19.50</span>

        <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>

    </div>

    

    </div>

</section>


    <div class="shop-items">

        <div class="shop-item">

            <span class="shop-item-title">Album 6</span>&nbsp;

            <div class="shop-item-details">

<span class="shop-item-price">£19.50</span>

            <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>

        </div>

  

   <div class="shop-items">

        <div class="shop-item">

            <span class="shop-item-title">Album 8</span>&nbsp;

            <div class="shop-item-details">

<span class="shop-item-price">£19.50</span>

            <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>

        </div>

   


慕尼黑5688855
浏览 87回答 1
1回答

慕森卡

在代码末尾,将此代码编辑为totaltotal.toFixed(2)编辑:在代码的最后一行function updateCartTotal() {var cartItemContainer = document.getElementsByClassName('cart-items')[0]var cartRows = cartItemContainer.getElementsByClassName('cart-row')var total = 0for (var i = 0; i < cartRows.length; i++) {&nbsp; &nbsp; var cartRow = cartRows[i]&nbsp; &nbsp; var priceElement = cartRow.getElementsByClassName('cart-price')[0]&nbsp; &nbsp; var quantityElement = cartRow.getElementsByClassName('cart-quantity-input')[0]&nbsp; &nbsp; var price = parseFloat(priceElement.innerText.replace('£', ''))&nbsp; &nbsp; var quantity = quantityElement.value&nbsp; &nbsp; total = total + (price * quantity)}total = Math.round(total * 100) / 100document.getElementsByClassName('cart-total-price')[0].innerText = '£' + total.toFixed(2)}
随时随地看视频慕课网APP

相关分类

Html5
我要回答