简体   繁体   English

事件侦听器“点击”需要点击两次才能调用 function

[英]Event Listener 'click' needing two clicks to call the function

I have the following code:我有以下代码:

if (this === declared.foodToChangeInput) {
            Utilities.deleteSecondInputListSuggestion()
            const html = sortedArray.map(food => {
                
                return `<li onclick="handleClickFirstInputListSuggestions(this.innerText)">${food.description}</li>`
            }).join('')
    
            declared.foodToChangeInputSuggestions.innerHTML = html
        } else if (this === declared.foodChoosenInput) {
            Utilities.deleteFirstInputListSuggestion()
            const html = sortedArray.map(food => {
                return `<li onclick="handleClickSecondInputListSuggestions(this.innerText)">${food.description}</li>`
            }).join('')
    
            declared.foodChoosenSuggestions.innerHTML = html
        }
    }

The problem is: i'm needing click two times in a item on the list so i can call the function. I also noticed that after the first click, the second works in any li, not only in the specifc li i clicked for the first time.问题是:我需要在列表中的一个项目中单击两次,以便我可以调用 function。我还注意到,在第一次单击之后,第二次在任何 li 中都有效,而不仅仅是在我单击的特定 li 中第一次。 When i press 'tab' and start focusing the list, the click works as it should be.当我按下“Tab”并开始关注列表时,点击会正常工作。

I changed the code removing the onclick and adding a addEventListener, example:我更改了删除 onclick 并添加一个 addEventListener 的代码,例如:

if (this === declared.foodToChangeInput) {
            Utilities.deleteSecondInputListSuggestion()
            const html = sortedArray.map(food => {
                
                return `<li>${food.description}</li>`
            }).join('')
    
            declared.foodToChangeInputSuggestions.innerHTML = html

            const list = foodToChangeInputSuggestions.querySelectorAll('li')

            list.forEach(li => li.addEventListener('click', event => {
                      handleClickFirstInputListSuggestions(event.target.innerText)}))


The code above also didn't work as expected.上面的代码也没有按预期工作。 To see the full code you can check my repository: https://github.com/nicolasjandre/foodexchange要查看完整代码,您可以查看我的存储库: https://github.com/nicolasjandre/foodexchange

And here you can see the problem (its when you digit something on input, it will show you the suggestions that match with the input.value and to click on it you need two clicks) https://nicolasjandre.github.io/foodexchange/在这里你可以看到问题(当你输入数字时,它会显示与 input.value 匹配的建议,点击它你需要点击两次) https://nicolasjandre.github.io/foodexchange /

I don't know if it can help, but the unique way that i was able to make the click works fine was create a const with a create element, add innerText and addEventListener to it and append this element.我不知道它是否有帮助,但我能够使点击正常工作的独特方法是创建一个带有 create 元素的 const,向其添加 innerText 和 addEventListener 以及 append 这个元素。 But append dont works like innerHTML = so didnt solve my problem (i was getting all the array as suggestions and also repeated ones)但是 append 不像 innerHTML = 所以没有解决我的问题(我得到了所有的数组作为建议,也重复了一些)

I discovered it!我发现了!

What was making this error was the event that i seted up:造成此错误的原因是我设置的事件:

declared.foodChoosenInput.addEventListener('change', Matches.displayMatches)
declared.foodToChangeInput.addEventListener('change', Matches.displayMatches)
declared.foodChoosenInput.addEventListener('keyup', Matches.displayMatches)
declared.foodToChangeInput.addEventListener('keyup', Matches.displayMatches)

As you guys can see, it has a 'change' listener.正如你们所看到的,它有一个“改变”监听器。 In this situation, when i was clicking the first time on the list, it call the displayMatches again and refresh the list with a new one.在这种情况下,当我第一次点击列表时,它会再次调用 displayMatches 并用新的刷新列表。 Because of that i was needing two clicks to call the func.因此,我需要点击两次才能调用函数。

I just removed the 'change' listener and know everything is working fine.我刚刚删除了“更改”侦听器,并且知道一切正常。 I also refactored a little the code, removed the onclick and started use addEventListener:我还重构了一点代码,删除了 onclick 并开始使用 addEventListener:

 if (this === declared.foodToChangeInput) {
            Utilities.deleteSecondInputListSuggestion()
          
            const html = sortedArray.map(food => {
              const li = document.createElement('li')
              li.innerText = food.description
              li.addEventListener('click', event => Exchange.handleClickFirstInputListSuggestions(event.target.innerText))
              
              return li
            })
            declared.foodToChangeInputSuggestions.replaceChildren(...html)
        }

The code above is for clean code only.上面的代码仅适用于干净的代码。 It didn't solve the problem.它没有解决问题。 The solution was remove the 'change' listener.解决方案是删除“更改”侦听器。

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

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