如何解决如何在 DOM 操作中结合 2 forEach vanilla JS
我正在做一个练习,如果价格 > 3,00,我需要更改产品的背景颜色。 这是我到目前为止所做的。出于某种原因,如果我要更改价格颜色,它将完美运行,但是,当尝试更改 div backgroundColor 时,它会更改所有卡片。
我做错了什么?
这里是网站:[https://www.monoprix.fr/courses/legumes-frais-mpx_c0000400?page=22]
谢谢
function exercise_3() {
var price = document.querySelectorAll(".grocery-item__normal-price");
let changePriceColor = 300;
var block = document.querySelectorAll(".grocery-item-item");
block.forEach( item => {
price.forEach(price => {
var changeColor = price.innerHTML.slice(0,4).replace(/,/g,'');
( changeColor > changePriceColor ) ?
item.style.backgroundColor = "red" : ''; (not working)
( changeColor > changePriceColor ) ?
price.style.color = "red" : ''; (working)
})
})
}
var numberOfPriceElements = 0;
// count elements on load
numberOfPriceElements = document.querySelectorAll(".grocery-item__normal-price").length;
document.addEventListener('scroll',(e) => {
// if count of elements is more then do the function and save the amount to the variable.
if(document.querySelectorAll(".grocery-item__normal-price").length > numberOfPriceElements) {
exercise_3();
numberOfPriceElements = document.querySelectorAll(".grocery-item__normal-price").length;
}
});
exercise_3();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。