我需要帮助 :) 我试图在其他主题中找到解决方案,但没能找到,所以如果有的话,我很抱歉。我是新手。
所以,问题。我有一堆div。我需要为每个 div 添加“活动”类,然后在 2 秒内将其删除。
我有解决方案,但我的主人说“从”然后“的”一点都不好。任何想法如何以其他方式完成?先感谢您!
const boxes = document.querySelectorAll('.box')
const innerBoxes = Array.from(boxes);
const clickOnDiv = fromEvent(innerBoxes, 'click')
.pipe(
map(event => {
let notActiveElements = [];
for ( let i=0; i < innerBoxes.length; i++) {
if(innerBoxes[i] != event.target) {
notActiveElements.push(innerBoxes[i])
}
}
return notActiveElements
}),
concatMap(element => from(element)
.pipe(
concatMap(element => of(element)
.pipe(
tap(el => console.log(el)),
delay(2000)
)),
tap(item => {
item.classList.add('active')
}),
delay(2000),
tap(item => {
item.classList.remove('active')
})
))
)
clickOnDiv.subscribe()