小熊奶糖(BearCandy)
小熊奶糖(BearCandy)
发布于 2024-02-22 / 7 阅读
0
0

JavaScript小测验1

JavaScript小测验

要求实现增加购物车,总金额显示在console中,可以清空购物车,查看余额,在字符串中使用变量

```

<head>

    <title>測驗</title>

    <style>

        button{

            font-size:25px;

            margin-right:10px;

        }

        p{

            font-size: 25px;

            display: inline-block;

            margin-right:10px;

        }

        img{

            height: 200px;

            display: block;

            margin-top:50px;

        }

    </style>

</head>

<body>

    <button onclick=" console.log(total);">購物車總額</button>

    <button onclick="total=0

    console.log('清空购物车');

    console.log('总金额:  ' + total);

    ">清空購物車</button>



    <img src="product\_images/product\_image1.jpg">

    <p>\$100</p>

    <button onclick="total = total+100

    console.log('总金额:  '+total);

    ">加入購物車</button>



    <img src="product\_images/product\_image2.jpg">

    <p>\$200</p>

    <button onclick="total = total+200

    console.log('总金额:  '+total);

    ">加入購物車</button>




    <script>

        let total = 0;

        let price = 0;

        total = price +total;

        let name;

        //使用反引号\`\`可以在字符串中使用变量ESC键下面的按键

        //可使用下面的方法输出变量

        let result=\`当前金额为\${total}其他内容\`

       console.log(result);

    </script>

</body>

```

总结

onclick和变量的使用。以及对代码的理解程度和熟练度。

**如何在字符串中使用变量**

  1. 使用运算符"+"
  2. 使用反引号"`"

评论