简体   繁体   English

Javascript 事件监听器需要点击两次,这样 function 才能工作

[英]Javascript eventlistener need to click twice so function will work

So when I click on the "hartje" section I am triggering a function.因此,当我单击“hartje”部分时,我触发了 function。 The function is getting all classes with the name "food-option". function 正在获取名为“food-option”的所有课程。 If I click on the function trigger it is supposed to give the "data-favorite" a value.如果我点击 function 触发器,它应该给“数据收藏”一个值。 It does give it a value, but the problem is that I have to click twice so it gets one它确实给了它一个值,但问题是我必须单击两次才能得到一个

 const foodOption = document.querySelectorAll(".food-option") function makeFavo() { for (var i = 0; i < foodOption.length; i++) { foodOption[i].addEventListener("click", e => { if (e.path[2].dataset.favorite == "fav") { e.path[2].dataset.favorite = "" e.path[1].children[0].style.backgroundColor = "gray" e.path[1].children[1].style.backgroundColor = "gray" e.path[1].style.backgroundColor = "gray" } else { e.path[2].dataset.favorite = "fav" e.path[1].children[0].style.backgroundColor = "red" e.path[1].children[1].style.backgroundColor = "red" e.path[1].style.backgroundColor = "red" } }) } }
 <section data-favorite="" class="food-option"> <section class="food-picture"> <figure> <img class="picture" src="./Images/vegetarisch-recept-flatbreads-met-falafel-yoghurt-muntsaus2-1585741275.jpg" alt=""> </figure> </section> <section class="food-choice"> <p>falafel</p> </section> <section class="favo-food"> <section class="hartje" onclick="makeFavo()"> <section class="hartje2"> </section> <section class="hartje3"> </section> </section> </section>

It's because two events are needed to trigger the event listener that adds the data attribute.这是因为需要两个事件来触发添加数据属性的事件监听器。

Click 1 all you do is add the event listener to each food option section.单击 1 您所做的就是将事件侦听器添加到每个食物选项部分。

Click 2 you are then triggering the event you just added which adds the data attribute.单击 2,然后您将触发您刚刚添加的添加数据属性的事件。

Just remove the function makeFavo and the onclick trigger and just add event listeners to the food options.只需删除 function makeFavo 和 onclick 触发器,然后将事件侦听器添加到食物选项即可。

The problem with your sample code is the way you have the function makeFavo() attached with onclick event on section element as well as having an addEventListener logic inside that function.您的示例代码的问题在于您将 function makeFavo()附加到 section 元素上的 onclick 事件以及在 ZC1C425268E68385D1AB5074C17A 内部具有 addEventListener 逻辑的方式。

In your function, there's an addEventListener for click, which means you are registering a new click event listener every time you call that function.在您的 function 中,有一个用于单击的addEventListener ,这意味着您每次调用 function 时都在注册一个的单击事件侦听器。 This results to what you're seeing about having to click twice to see the background color changing, but internally, if you take a closer look, it's really executing multiple registered click events.这导致您看到必须单击两次才能看到背景颜色的变化,但在内部,如果您仔细观察,它实际上是在执行多个已注册的单击事件。

What you can do is to drop either the onclick or the addEventListener to prevent your code from firing multiple events.您可以做的是删除 onclick 或 addEventListener 以防止您的代码触发多个事件。

See my example below.请参阅下面的示例。 I tried to keep most of your code:我试图保留你的大部分代码:

HTML ( I removed the onclick here... ) HTML我在这里删除了 onclick ......

<section data-favorite="" class="food-option">
<section class="food-picture">
  <figure>
    <img class="picture"
         src="./Images/vegetarisch-recept-flatbreads-met-falafel-yoghurt-muntsaus2-1585741275.jpg"
         alt="">
  </figure>
</section>
<section class="food-choice">
  <p>falafel</p>
</section>
<section class="favo-food">
  <section class="hartje">
    <section class="hartje2">
      2
    </section>
    <section class="hartje3">
      3
    </section>
  </section>
</section>

JavaScript JavaScript

 // Attach the click listener when DOM is ready.
 document.addEventListener('DOMContentLoaded', (e) => {
  // updated from querySelectorAll to querySelector since there's only 1 element in your HTML for this one.
  const foodOption = document.querySelector(".food-option");
  foodOption.addEventListener("click", e => {
    if (e.path[2].dataset.favorite == "fav") {
      e.path[2].dataset.favorite = ""
      e.path[1].children[0].style.backgroundColor = "gray"
      e.path[1].children[1].style.backgroundColor = "gray"
      e.path[1].style.backgroundColor = "gray"
    } else {
      e.path[2].dataset.favorite = "fav"
      e.path[1].children[0].style.backgroundColor = "red"
      e.path[1].children[1].style.backgroundColor = "red"
      e.path[1].style.backgroundColor = "red"
    }
  });
});

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

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