我的箭头函数在三元运算符中不起作用,而且它在控制台中没有给我任何错误
下面是我的代码
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/