简体   繁体   English

将 Function 作为 Javascript 中的参数传递 - 不工作

[英]Passing Function as argument in Javascript - Not Working

I have run into a problem on my project for a popup that I am building.我在我的项目中遇到了我正在构建的弹出窗口的问题。 The goal is to create a function that will trigger an html popup.目标是创建一个 function 将触发 html 弹出窗口。 This Javascript function takes 5 arguments: Text, Button 1 text, Button 2 text, Button 1 action, and Button 2 action.此 Javascript function 需要 5 个 arguments:文本、按钮 1 文本、按钮 2 文本、按钮 1 操作和按钮 2 操作。 For the button actions, I want to pass functions for arguments although for the life of me, I can't figure out why it it no working.对于按钮操作,我想传递 arguments 的函数,尽管在我的一生中,我无法弄清楚为什么它不起作用。 Here is all the code.这是所有的代码。

Index.html索引.html

<div class="overlay" id = "popup2" style = "display: none;">
    <div class = "popup">
        <div id = "popup_text_box">
            <h id = "popup_text2"></h>
        </div><br><br>
        <div class = "btn" id = "popup_btn1" style="background-color:#D90505;"></div><br><br>
        <div class = "btn" id = "popup_btn2" style="background-color:#BD51FF;" ></div><br><br>
    </div>
</div>

Play.js Play.js

function popUp2(text, btn1, btn2, action1, action2){
        document.getElementById("popup2").style.display = "block";
        document.getElementById("popup_text2").innerHTML = text;
        document.getElementById("popup_btn1").innerHTML = btn1;    
        document.getElementById("popup_btn2").innerHTML = btn2;  
        document.getElementById("popup_btn1").onclick = action1();    
        document.getElementById("popup_btn2").onclick = action2();    
}

popUp2("You have been invited to join this club", "Accept", "Decline", joinClub, closePopUp);

The arguments, joinClub, and closePopUp, are actually arguments. arguments、joinClub 和 closePopUp,实际上是 arguments。 But unfortunately, they are not working whatsoever.但不幸的是,他们没有任何工作。 Also, I removed the functions and the popup worked fine, so I know that the problem lies with the functions being passed as arguments.此外,我删除了这些函数并且弹出窗口运行良好,所以我知道问题在于作为 arguments 传递的函数。 Any solution would be amazing because this small glitch is hindering me from completing my project.任何解决方案都会令人惊叹,因为这个小故障阻碍了我完成我的项目。 Thank you!谢谢!

Remove the () from action1 and action2 in your function.从 function 中的action1action2中删除() You want to assign the click handler the function reference, not the return value of the function.您想为点击处理程序分配 function 引用,而不是 function 的返回值。

Here's a working example:这是一个工作示例:

 function popUp2(text, btn1, btn2, action1, action2) { document.getElementById("popup2").style.display = "block"; document.getElementById("popup_text2").innerHTML = text; document.getElementById("popup_btn1").innerHTML = btn1; document.getElementById("popup_btn2").innerHTML = btn2; document.getElementById("popup_btn1").onclick = action1; document.getElementById("popup_btn2").onclick = action2; } let joinClub = function() { console.log("joinClub called"); } let closePopUp = function() { console.log("closePopUp called"); } popUp2("You have been invited to join this club", "Accept", "Decline", joinClub, closePopUp);
 <div class="overlay" id="popup2" style="display: none;"> <div class="popup"> <div id="popup_text_box"> <h id="popup_text2"></h> </div><br><br> <div class="btn" id="popup_btn1" style="background-color:#D90505;"></div><br><br> <div class="btn" id="popup_btn2" style="background-color:#BD51FF;"></div><br><br> </div> </div>

function popUp2(text, btn1, btn2, action1, action2){
        document.getElementById("popup2").style.display = "block";
        document.getElementById("popup_text2").innerHTML = text;
        document.getElementById("popup_btn1").innerHTML = btn1;    
        document.getElementById("popup_btn2").innerHTML = btn2;  
        document.getElementById("popup_btn1").onclick = action1;    
        document.getElementById("popup_btn2").onclick = action2;    
}

Assigning onclick with action2() makes it run the function and closes the popup while assigning the popup buttons null because they are void functions.使用 action2() 分配 onclick 使其运行 function 并在分配弹出按钮 null 时关闭弹出窗口,因为它们是无效函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM