简体   繁体   English

如何使用传递给 JavaScript 中的 function 的参数添加点击事件侦听器

[英]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.

相关问题 如何使用带有事件参数和参数的函数添加和删除事件侦听器? - How do I add and remove an event listener using a function with event argument and parameters? 如何使用带参数的函数添加和删除事件侦听器? - How do I add and remove an event listener using a function with parameters? 如何添加此事件侦听器以在单击时删除弹出窗口? - How do I add this event listener that removes a popup on click? Javascript 如何在正文中添加单击事件侦听器? - Javascript how to add a click event listener in the body? jQuery:如何将点击侦听器与Submit事件功能结合在一起? - Jquery: How do I combine a click listener with a submit event function? 事件参数未传递给JavaScript函数 - Event argument not passed to JavaScript function 如何添加事件侦听器以使用常规 javascript 检查单选按钮? - How do I add an event listener for checking radio buttons using regular javascript? 如何将事件监听器添加到我的javascript函数中 - How to add event listener to my javascript function 如何计算使用 javascript 中的事件侦听器单击的 div - How do I count divs clicked using an event listener in javascript 如何删除作为匿名函数传递的事件侦听器? - How do I remove an event listener that has been passed in as an anonymous function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM