Introduction( kind of :) )

首先,一杯咖啡要generate 5 coffee颗豆子.这个咖啡类有属性Strong,我想要grey out个咖啡豆,它是> strength(如下图所示).

My CoffeeMachine Class (simplified):

class CoffeeMachine {
    constructor() {
        this.name = ""
        this.coffeeList = { list: [] }
        this.tokenList = { list: [] }
        this.shoppingCard = { list: [] }
    }
    addCoffe(coffee) {
        this.coffeeList.list.push(coffee)
    }
    addToken(token) {
        this.coffeeList.list.push(token)
    }
    addToCart(item) {
        this.shoppingCard.list.push(item)
    }
}

My Coffee Class (simplified):

class Coffee {
    constructor() {
        this.name = ""
        this.price = 0
        this.time = 0.0
        this.imgsrc = ""
        this.strength = 0
        this.sugar = 0
        this.caffeine = 0
        this.values = []
        this.titelArray = ["caffeeine", "sugar", "time", "strength"]
        this.colors = ["#e34444", "#7944e3", "#44e35c", "#e3d044"]
    }
    setValues(name, price, time, strength, imgsrc, sugar, caffeine) {
        this.name = name
        this.price = price
        this.time = time
        this.strength = strength
        this.imgsrc = imgsrc
        this.sugar = sugar
        this.caffeine = caffeine
    }
    setCoffeeValues() {
        this.values = [this.caffeine + "mg", this.sugar + "g", this.time + "s", this.strength + "/5"]
    }

}

咖啡将被推入咖啡机的购物车 list 中,如下所示:

coffeeMachine.shoppingCard.list.push(coffeeMachine.coffeeList.list[index])

Init the coffees

coffeeLatte.setValues("Latte Macchiato", 1.60, 30.0, 3, "../pics/coffeeLatte.png", 18, 75)
        coffeeBlack.setValues("Black Coffee", 1.20, 20.0, 5, "../pics/coffeeBlack.png", 4, 95)
        coffeCappunchino.setValues("Cappuccino", 1.60, 35, 2, "../pics/coffeeCappuchino.png", 12, 64)

The update shopping cart method in the coffee machine class (the prolem):

下面是我为列表中的每一种咖啡生成咖啡豆的方式:

  for (let i = 0; i < this.shoppingCard.list.length; i++) {
            for (let j = 0; j < 5; j++) {
                document.getElementsByClassName('beans')[i].innerHTML += `<img class="bean" src="../pics/bean.png"></img>`
            }
        }

在下面的代码中,我将One&lt;=Cafee.Stronga的不透明度设置为100%,将其他代码的不透明度设置为30%:

for (let i = 0; i < this.shoppingCard.list.length; i++) {
    for (let j = 0; j < this.shoppingCard.list[i].strength; j++) {
        document.getElementsByClassName('bean')[i].style.opacity = "1"
        if (j <= this.shoppingCard.list[i].strength){
            for (let k = this.shoppingCard.list[i].strength; k < 5; k++) {
                document.getElementsByClassName('bean')[k].style.opacity = "0.3"

            }
        }
    }
}  

After adding a few coffes to the shopping cart this happens: enter image description here

推荐答案

我想这可以解决你的问题

for (let j = 1; j <= this.shoppingCard.list[i]; j++) {
    for (let k = 0; k < 5; k++) {
        if (k + 1 < this.shoppingCard.list[i].strength) {
            document.getElementsByClassName('bean')[0].children[k].style.opacity = "1"
        } else {
            document.getElementsByClassName('bean')[0].children[k].style.opacity = "0.3"
        }
    }
}

Javascript相关问答推荐

如何最好地从TypScript中的enum获取值

窗口.getComputedStyle()在MutationObserver中不起作用

为什么我的列表直到下一次提交才更新值/onChange

Angular material 拖放堆叠的牌副,悬停时自动展开&

为什么!逗号和空格不会作为输出返回,如果它在参数上?

Promise Chain中的第二个useState不更新

查找最长的子序列-无法重置数组

加载背景图像时同步旋转不显示的问题

我怎么在JS里连续加2个骰子的和呢?

如何将多维数组插入到另一个多维数组中?

将核心模块导入另一个组件模块时存在多个主题

使用Google API无法进行Web抓取

如何将innerHTML字符串修剪为其中的特定元素?

如何在 Select 文本时停止Click事件?

回溯替代方式

删除加载页面时不存在的元素(JavaScript)

Django模板中未加载JavaScript函数

如何在加载页面时使锚定标记成为焦点

正在发出错误的URL请求

Node.js的Fetch()调用总是创建新的Express会话