子元素触发其父元素的onClick事件。

huangapple go评论70阅读模式
英文:

Child triggers the onClick event of its parent

问题

我有一个父元素div,我在其上设置了一个onClick事件处理程序,但它被其子元素触发。我只想在单击父元素时筛选出父元素的一个子元素。我将使用状态创建一个多选列表,这就是我需要的原因。

这是我的代码:

import { useState, useEffect } from "react";
import events from "../utils/events.js";

import "../styles/events.scss";

const Events = () => {
  const [selectedEvent, setSelectedEvent] = useState(events[0].id);

  return (
    <div className="events-container">
      <div className="events">
        <div className="events-cards">
          {events.map((event) => (
            <div
              key={event.id}
              className="event"
              onClick={(e) => {
                const date = e.target.querySelector(".date");
                date.classList.add("selected-event");
              }}
            >
              <p className="date">{event.date}</p>
              <h1 className="name">{event.name}</h1>
              <p className="description">{event.description}</p>
            </div>
          ))}
        </div>
      </div>
    </div>
  );
};

export default Events;

希望这有助于你的问题。

英文:

I have a parent, div, on which I've set an onClick event handler, but it is trigerred by its childrens. All I want to do is to filter one child of the parent element when the parent is clicked. I will do a list o multiple choices, using states, so that's why I need it.

Here's my code:

import { useState, useEffect } from &quot;react&quot;;
import events from &quot;../utils/events.js&quot;;

import &quot;../styles/events.scss&quot;;

const Events = () =&gt; {
  const [selectedEvent, setSelectedEvent] = useState(events[0].id);

  return (
    &lt;div className=&quot;events-container&quot;&gt;
      &lt;div className=&quot;events&quot;&gt;
        &lt;div className=&quot;events-cards&quot;&gt;
          {events.map((event) =&gt; (
            &lt;div
              key={event.id}
              className=&quot;event&quot;
              onClick={(e) =&gt; {
                const date = e.target.querySelector(&quot;.date&quot;);
                date.classList.add(&quot;selected-event&quot;);
              }}
            &gt;
              &lt;p className=&quot;date&quot;&gt;{event.date}&lt;/p&gt;
              &lt;h1 className=&quot;name&quot;&gt;{event.name}&lt;/h1&gt;
              &lt;p className=&quot;description&quot;&gt;{event.description}&lt;/p&gt;
            &lt;/div&gt;
          ))}
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
};

export default Events;

答案1

得分: 1

你可以在事件对象上使用stopPropagation()方法。这将阻止事件冒泡到DOM树的上层,并阻止它达到父元素。

onClick={(e) => {
    // 阻止点击事件传播到父div
    e.stopPropagation();

    const date = e.currentTarget.querySelector(".date");
    date.classList.add("selected-event");
}}
英文:

you can use the stopPropagation() method on the event object. This will stop the event from bubbling up the DOM tree and prevent it from reaching the parent element.

onClick={(e) =&gt; {
            // Stop the click event from reaching the parent div
            e.stopPropagation();

            const date = e.currentTarget.querySelector(&quot;.date&quot;);
            date.classList.add(&quot;selected-event&quot;);
          }}

huangapple
  • 本文由 发表于 2023年7月24日 19:36:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定