如何在功能参数中使用可更改值?

发布于 2025-01-18 13:54:22 字数 482 浏览 5 评论 0 原文

这很难解释,但是我知道为什么我尝试的是不起作用,我只想知道是否有解决方案。我希望您能够理解我想通过的代码做什么,

async function copyInvite() {
    let buttons = document.querySelectorAll("button.rl-btn-invite");

    addEventListenerLoop(buttons, 'click', event => { navigator.clipboard.writeText(node.id); });
}



async function addEventListenerLoop(nodes, type, code) {
    for (const node of nodes) {
        node.addEventListener(type, code);
    }
}

我试图根据

我尝试使用可变值的 节点在函数上更改的值尝试得足够好。

it's pretty hard to explain, but I know why what I tried doesn't work I just want to know if there is a solution. I hope you can understand what I want to do through the code

async function copyInvite() {
    let buttons = document.querySelectorAll("button.rl-btn-invite");

    addEventListenerLoop(buttons, 'click', event => { navigator.clipboard.writeText(node.id); });
}



async function addEventListenerLoop(nodes, type, code) {
    for (const node of nodes) {
        node.addEventListener(type, code);
    }
}

I am trying to make a value in the function change based on what node I am doing it on

I tried using mutable values but it either didn't work or I didn't try well enough.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

噩梦成真你也成魔 2025-01-25 13:54:22

给定您的示例,最简单的解决方案是使用 而不是 node ,请参考处理程序绑定到:

addEventListenerLoop(
  buttons,
  'click',
  event => { navigator.clipboard.writeText(event.currentTarget.id); }
  //                                       ^^^^^^^^^^^^^^^^^^^
);

Given your example, the simplest solution would be to use event.currentTarget instead of node to refer to the element the handler was bound to:

addEventListenerLoop(
  buttons,
  'click',
  event => { navigator.clipboard.writeText(event.currentTarget.id); }
  //                                       ^^^^^^^^^^^^^^^^^^^
);
女中豪杰 2025-01-25 13:54:22

如果我正确理解您,您正在寻找一个解决方案,以便可以在代码>代码回调中访问当前 node

为此,您可以尝试嵌套回调方法,其中外部收到当前节点并返回此特定 node 的实际事件处理程序。

看起来像这样:

async function copyInvite() {
    let buttons = document.querySelectorAll("button.rl-btn-invite");

    addEventListenerLoop(buttons, 'click', node => event => { navigator.clipboard.writeText(node.id); });
}


async function addEventListenerLoop(nodes, type, code) {
    for (const node of nodes) {
        node.addEventListener(type, code(node));
    }
}

If I understand you right, you are looking for a solution to have access to the current node in your code callback.

For this, you can try to nest your callback methods, where the outer one receives the current node and returns the actual event handler for this specific node.

This will look like this:

async function copyInvite() {
    let buttons = document.querySelectorAll("button.rl-btn-invite");

    addEventListenerLoop(buttons, 'click', node => event => { navigator.clipboard.writeText(node.id); });
}


async function addEventListenerLoop(nodes, type, code) {
    for (const node of nodes) {
        node.addEventListener(type, code(node));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文