简体   繁体   English

动态生成的元素不触发事件监听器

[英]Dynamically generated element not firing event listener

guys are am having an issue that should be easy but it doesn't seem like anything is working, I have a simple site that I am trying to register an on click for a table row with.伙计们遇到了一个应该很容易的问题,但似乎没有任何工作,我有一个简单的网站,我正在尝试注册一个表格行的点击。 The content is dynamically generated from Javascript and gets it from firebase :内容是从 Javascript 动态生成的,并从 firebase 获取:

                        var tableRow = document.createElement('tr');
                    var tableData = document.createElement('td');
                    tableData.addEventListener("click", draftRider(doc.data().name))
                    tableData.innerHTML = "<p class='test'> click me </p>"
                    tableRow.appendChild(tableData);
                    document.getElementById("draftingStartBlock").appendChild(tableRow);

I have tried an escaped string, onclick on event and now add event listener.我尝试了一个转义字符串,onclick 事件,现在添加事件侦听器。 Also when the page first load the function appears to fire.此外,当页面首次加载时,该功能似乎会触发。

Here is the function I want to call on click :这是我想在单击时调用的函数:

            function draftRider(riderName) {

            showSnackBar(riderName)


            if (playersTurn) {
                var riderQuery = firebase.firestore().collection("leagues").doc(userLeagueId).collection("rider_list")

                riderQuery.get().then(function (doc) {
                    if (doc.exists) {

                        var currentRiderName = doc.data().rider_name;
                        var draftedBy = doc.data().drafted_by;

                        if (riderName === currentRiderName && draftedBy === "") {

                            database.collection("leagues")
                                .doc(userLeagueId)
                                .collection("rider_list")
                                .doc(doc.id)
                                .update("Drafted_By", userId)

                            setNextPlayerAsPickingPlayer()
                        }

                    } else {
                        // doc.data() will be undefined in this case
                        console.log("No such document!");
                    }
                }).catch(function (error) {
                    console.log("Error getting document:", error);
                });

            } else {
                showSnackBar("Its currently not your turn");
            }
        }

This line in your code is the issue:您代码中的这一行是问题所在:

 tableData.addEventListener("click", draftRider(doc.data().name))

addEventListener is a function that expects an event type as the first argument and a function as the second. addEventListener是一个函数,它需要一个事件类型作为第一个参数,一个函数作为第二个参数。 In your case, you aren't passing the function as the second argument, you are actually calling the function and passing the value it returns (undefined in this case) as the second argument.在您的情况下,您没有将函数作为第二个参数传递,您实际上是在调用该函数并将其返回的值(在本例中未定义)作为第二个参数传递。 Which is why you are seeing your function called once when the page loads.这就是为什么您会在页面加载时看到您的函数调用一次的原因。

Based on the code in your post, you could solve it with:根据您帖子中的代码,您可以使用以下方法解决:

tableData.addEventListener("click", function() {
   draftRider(doc.data().name)
})

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

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