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

Fre*_*eon 3 html javascript

我的箭头函数在三元运算符内不起作用,并且在控制台中也没有给出错误

下面是我的代码

Holders 数组中的对象是 Html 元素

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')
            }


    })

}

Run Code Online (Sandbox Code Playgroud)

mon*_*001 5

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

(() => {
            holder.name.style.backgroundColor = holderbg
            holder.target.classList.remove('hide')
        })()
Run Code Online (Sandbox Code Playgroud)

  • 很高兴有帮助。不用担心 downwote 的攻击。目的是互相帮助,而不是名誉。;o) (3认同)
  • 又短又甜!感谢@monkey-0001 和@freduah-gideon! (2认同)