如何引用一个带有我想提供的参数的函数?

huangapple go评论51阅读模式
英文:

How can I reference a function with the arguments I want to provide?

问题

You can achieve a similar effect in JavaScript by using an anonymous function as the onclick handler, which can then call your cancelReturn function with the desired arguments. Here's how you can do it:

return_btn = document.getElementById(bkid).querySelector(".returnbtn");
return_btn.onclick = function() {
    cancelReturn(bkid);
};
return_btn.innerHTML = "Cancel";

This code sets the onclick handler of the return_btn element to an anonymous function that calls cancelReturn(bkid) when the button is clicked. This way, you can pass the bkid argument to your cancelReturn function.

英文:

This is my code right now.

 return_btn = document.getElementById(bkid).querySelector(".returnbtn");
 return_btn.onclick = cancelReturn; // I want to define arguments here
 return_btn.innerHTML = "Cancel"

Here, I wish to define the onclick function as cancelReturn while providing arguments.
For example in HTML I would write

<button class="returnbtn" onclick=cancelReturn(bkid)>Cancel</button>

How can I do something similar in javascript?

Thank you for reading this, I hope you can help me figure this out.

答案1

得分: 1

使用箭头函数调用cancel_return

return_btn.onclick = () => cancelReturn(args);
英文:

Use an arrow function to call cancel_return

return_btn.onclick = () => cancelReturn(args);

huangapple
  • 本文由 发表于 2023年4月17日 09:52:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定