gpt4 book ai didi

javascript - 箭头函数在三元运算符中不起作用

转载 作者:行者123 更新时间:2023-12-05 02:04:28 24 4
gpt4 key购买 nike

我的箭头函数在三元运算符中不起作用,而且它在控制台中没有给我任何错误

下面是我的代码

The Objects in the Holders Array Are Html Elements

const Holders =
[
{
name: accountHolder,
target: accountHelper
},
{
name: helpHolder,
target: helpContainer
}
]
const holderbg = 'rgba(128,128,128,.1)'
document.querySelector('html').onclick = (event) => {
Holders.map(holder => {
holder.name.getAttribute('role') == event.target.getAttribute('role') ?
// The Arrow Function Does Not Work But The Code Runs When It is not in the function
// replacing ()=>{} with just holder.name.style.backgroundColor = holderbg works
() => {
holder.name.style.backgroundColor = holderbg
holder.target.classList.remove('hide')
}

:
// The Arrow Function Does Not Work But The Code Runs When It is not in the function
// replacing ()=>{} with just holder.name.style.backgroundColor = 'initial' works
() => {
holder.name.style.backgroundColor = 'initial'
holder.target.classList.add('hide')
}


})

}

最佳答案

你必须执行你声明的函数:

(() => {
holder.name.style.backgroundColor = holderbg
holder.target.classList.remove('hide')
})()

关于javascript - 箭头函数在三元运算符中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64303282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com