[英]How do I add an on click event listener using an argument passed to a function in JavaScript
Here are my functions:这是我的功能:
function type(message) {
document.getElementById("body").innerHTML="<div id=\"textbox\" class=\"textbox\"></div><div id=\"oc\" class=\"option-container\"></div>"
for (let i=1; i<9; i++) {
document.getElementById("oc").innerHTML+="<div id=\"o"+i+"\" class=\"option\"></div>"
}
printLetterByLetter("textbox", message, 15)
}
function createop(text, program) {
for (let i=1; i<9; i++) {
if (!document.getElementById("o"+i).classList.contains("filled")) {
document.getElementById("o"+i).classList.add("filled")
document.getElementById("o"+i).innerText=text
document.getElementById("o"+i).addEventListener("click", ()=> {program})
i=9
}
}
}
The type function is always called before the createop function. function 类型总是在 createop function 之前调用。
I want to run whatever the program
argument contains when that HTML object is clicked on.当单击 HTML object 时,我想运行
program
参数包含的任何内容。 The way I did it above does not work.我上面的方法不起作用。 How can I do this?
我怎样才能做到这一点?
The code in its entirety can be found here: https://github.com/Fish767/Feedback-Nightmare/tree/master and the two functions above are located in the asterfix.js file which is in the js folder.完整的代码可以在这里找到: https://github.com/Fish767/Feedback-Nightmare/tree/master上面的两个函数位于 asterfix.js 文件中,该文件位于 js 文件夹中。
TL;DR TL;博士
You are missing parentheses to call the function.您缺少调用 function 的括号。 Solution code:
解决方案代码:
function createop(text, program) {
for (let i=1; i<9; i++) {
if (!document.getElementById("o"+i).classList.contains("filled")) {
document.getElementById("o"+i).classList.add("filled")
document.getElementById("o"+i).innerText=text
document.getElementById("o"+i).addEventListener("click", ()=> {program()})
i=9
}
}
}
Long answer: By not putting the parentheses after program
, you are just writing a serialized form of the function and then ending the line.长答案:通过不将括号放在
program
之后,您只是在编写 function 的序列化形式,然后结束该行。 The following program shows serialized vs called (without parens vs with).以下程序显示了序列化与调用(不带括号与带)。
function abc() { return "abc"; } console.log("Without parens: " + abc); console.log("With parens: " + abc());
Edit: You are calling the creatop
function like this:编辑:您正在调用
creatop
function ,如下所示:
createop("Try Again","stop=true;updateGUI4();")
However it is not good practice to use strings and eval()
them (what you are trying to do here will fail in strict mode)但是,使用字符串和
eval()
它们不是一个好习惯(您在此处尝试执行的操作将在严格模式下失败)
Alternatively, you could call the function like this:或者,您可以像这样调用 function:
createop("Try Again",()=>{let stop=true;updateGUI4();})
which uses anonymous functions to allow the above solution to work.它使用匿名函数来允许上述解决方案工作。 However, this will not change the value of the
stop
variable.但是,这不会改变
stop
变量的值。
This can be solved by slightly modifying the for loop in the createop function.这可以通过稍微修改 createop function 中的 for 循环来解决。 If we remove all the options that have no code in them, then we can recreate them and add code to the first one.
如果我们删除所有没有代码的选项,那么我们可以重新创建它们并将代码添加到第一个选项。 The options with code have the class
filled
and we can use this to determine which options are empty.带有代码的选项
filled
了 class,我们可以使用它来确定哪些选项是空的。
function createop(text, program) {
//used in if statement later
let temporarycode=program
for (let i=1; i<9; i++) {
//This determines which options are filled. It selects all that are not
if (!document.getElementById("o"+i).classList.contains("filled")) {
//delete the empty option and create a new option that contains the passed arguments
document.getElementById("o"+i).remove()
document.getElementById("oc").innerHTML+="<div id=\"o"+i+"\" class=\"option\" onclick=\""+temporarycode+"\">"+text+"</div>"
//this is used to determine if the option is the first one modified
if (temporarycode===program) {
//adds the filled class so we don't modify this with future additions
document.getElementById("o"+i).classList.add("filled")
}
//replace the arguments so that each option after the first is empty
temporarycode=""
text=""
}
}
}
By removing each empty option and adding them again we maintain the proper number of options desired and we keep them in the proper order.通过删除每个空选项并再次添加它们,我们可以保持所需的适当数量的选项,并将它们保持在正确的顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.