英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论