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

标签 javascript html

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

下面是我的代码

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/

相关文章:

javascript - 将 JQuery 转换为 JavaScript

javascript - 自动填充输入字段的最佳方法是什么?

html - 关键帧动画在 IE11 上无法正常工作

html - 无法定位 div 第一个按钮 CSS

javascript - 如果内部按钮单击,则防止 anchor 导航

javascript - 在 MarkLogic 9 中出现 XDMP-EXPNTREECACHEFULL 错误

Javascript for/in 循环 - 连接来自两个不同对象的项目?

javascript - 如何在每个动态创建的 ImageButton 周围包裹一个超链接? Visual Studio

php - 借助 PHP、MySQL 和 HTML 表创建分类账户

javascript - 使用 Brad Birdsall 的 Swipe 2.0 在纯 JavaScript 中的 iOS 导航栏