JavaScript setVaria可以成为功能

发布于 2025-02-10 03:54:02 字数 969 浏览 0 评论 0原文

我有一个工具栏,该工具栏具有4个值,在页面加载后需要分配。 电话,联系人,反馈,聊天前三个是数据元素,这些数据元素正常,但是第四chat需要不同的基于用户区域的不同。
因此,我的功能是一个开关语句

const handleChatClick = (e) => {
        console.log('handle chat click fired')
        switch(customerCountry) {
            case('US'):
            case('CA'):
            case('PR'): {
               triggerChat('NAM');
         }

,我正在使用setVariable分配所有内容,

    toolbarOnThePage.setAttribute('phone', `${phoneNumberVariable}`);
    toolbarOnThePage.setAttribute('contactus', `${contactUsVariable}`);
    toolbarOnThePage.setAttribute('feedback', `${feedbackVariable}`);
    toolbarOnThePage.setAttribute('chat', `${handleChatClick()}`); 

但是这将分配chat作为我函数的字符串版本。我尝试了我能想到的每种组合:
$ {()=> HandleChatClick()} HandlechatClick() $ {HandlechatClick} HandlechatClick如何将属性设置为函数?

I have a toolbar that has 4 values that need to be assigned after the page loads. phone, contactus, feedback, chat The first three are data elements that are being assigned just fine but the forth chat needs to be different based of off the user's region.

So I have a function that is a switch statement

const handleChatClick = (e) => {
        console.log('handle chat click fired')
        switch(customerCountry) {
            case('US'):
            case('CA'):
            case('PR'): {
               triggerChat('NAM');
         }

And I am assigning everything with setVariable

    toolbarOnThePage.setAttribute('phone', `${phoneNumberVariable}`);
    toolbarOnThePage.setAttribute('contactus', `${contactUsVariable}`);
    toolbarOnThePage.setAttribute('feedback', `${feedbackVariable}`);
    toolbarOnThePage.setAttribute('chat', `${handleChatClick()}`); 

However this will assign chat to be a string version of my function. I have tried every combination I can think of:
${() => handleChatClick()} handleChatClick() ${handleChatClick} handleChatClick How can I set the attribute to be a function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文