如何在功能参数中使用可更改值?
这很难解释,但是我知道为什么我尝试的是不起作用,我只想知道是否有解决方案。我希望您能够理解我想通过的代码做什么,
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);
}
}
我试图根据
我尝试使用可变值的 节点在函数上更改的值尝试得足够好。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给定您的示例,最简单的解决方案是使用 而不是
node
,请参考处理程序绑定到:Given your example, the simplest solution would be to use
event.currentTarget
instead ofnode
to refer to the element the handler was bound to:如果我正确理解您,您正在寻找一个解决方案,以便可以在
代码>代码
回调中访问当前node
。为此,您可以尝试嵌套回调方法,其中外部收到当前
节点
并返回此特定node
的实际事件处理程序。看起来像这样:
If I understand you right, you are looking for a solution to have access to the current
node
in yourcode
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 specificnode
.This will look like this: