简体   繁体   English

如何让我的“点击”事件处理程序引用这些特定元素?

[英]How can I get my 'click' event handler to reference these specific elements?

Here's a sandbox of a problem I've run into.这是我遇到的一个问题的沙箱 I'm using React:我正在使用反应:

    <div className="1" onClick={onClickHandler}>
     <div className="2">
      <img className="3">
     </div>
    </div>

In scenarioA, img wasn't present & onClickHandler 's e.target referenced element 2 .在场景 A 中, img不存在onClickHandlere.target引用了元素2 I believe this is because:我相信这是因为:

  1. '1' & '2' overlapped each other - when you clicked on one you clicked on the other. “1”和“2”相互重叠 - 当您点击一个时,您就点击了另一个。
  2. Like e.currentTarget , e.target will reference the 'highest' element you click on that has a 'click' handler - with an exception: if the element has a descendant that is also clicked on when you click, it'll reference the lowest descendant.e.currentTarget一样, e.target将引用您点击的具有“点击”处理程序的“最高”元素——有一个例外:如果该元素有一个在您点击时也被点击的后代,它将引用最低的后代。

The problem: in scenarioB, img has been added & now e.target refers to img .问题:在场景 B 中,添加了img并且现在e.target指的是img I need to refer to element 1 & 2 and intend to do this with the event handler staying on '1'.我需要参考元素12 ,并打算在事件处理程序保持在“1”的情况下执行此操作。

Solutions:解决方案:

  1. Somehow, in onClickHandler get it to see '2'.不知何故,在onClickHandler中让它看到“2”。

  2. Refactor img to be like '2'.img重构为“2”。 In my project, I think all this'd involve is making img have all the html attributes '2' has.在我的项目中,我认为所有这一切都涉及使img具有“2”具有的所有 html 属性。 However, I know having the attributes on '2' works - and even though I can't think of a way why this solution would break, I'm hesitant - especially if solution#1 works.但是,我知道“2”上的属性有效——尽管我想不出为什么这个解决方案会失败,但我很犹豫——尤其是如果解决方案#1 有效。


Summarizing the Question and Solution总结问题和解决方案

Below you can see two onClickHandler that logs out the event.target.您可以在下面看到两个注销 event.target 的 onClickHandler。

Expected : retrieve the parent node of the leaf nodes (children nodes without children nodes or innermost HTML element).预期:检索叶节点的父节点(没有子节点的子节点或最里面的 HTML 元素)。

Actual : retrieved the leaf nodes.实际:检索叶节点。

Fix : change event.target to event.target.parentNode .修复:将event.target更改为event.target.parentNode

import React, { useState, useEffect } from "react";
import "./styles.css";

export default function App() {
  function onClickHandler(event) {
    console.log(event.target); // THE FIX: change this to event.target.parentNode
  }
  return (
    <>
      <div className="one" onClick={onClickHandler}>
        <div className="two"></div>
      </div>

      <div className="one" onClick={onClickHandler}>
        <div className="two">
          <img className="three" />
        </div>
      </div>
    </>
  );
}

暂无
暂无

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

相关问题 如果click事件冒泡到绑定到同一点击处理程序的元素,我如何隔离点击的初始目标? - How can I isolate the initial target of a click, if the click event bubbles up to elements bound to the same click handler? Javascript单击事件处理程序 - 如何获取对单击项目的引用? - Javascript click event handler - how do I get the reference to the clicked item? 如何在不手动单击屏幕来激发事件的情况下进入我的事件处理程序? - How can I get inside my event handler without manually clicking the screen to stimulate an event? 如何从点击事件处理程序中获取ID? - How do I get an ID from a click event handler? 如何运行元素的click事件处理程序? - How can I run element's click event handler? 如何在此事件处理程序创建的元素的帮助下将元素动态添加到事件处理程序之外? - How can I dynamicaly append elements outside the event handler with the help of an element created by this event handler? 如何在事件处理程序中获取javascript事件对象? - How can I get the javascript event object inside a event handler? 如何将带有多个参数的函数引用传递给jQuery(document).on(&#39;click&#39;事件的回调? - How can I pass a function reference with multiple arguments to the callback of my jQuery(document).on('click' event? 如何将我的元素带入事件处理函数? - how can I carry my element into the event handler function? 如何获取通过在 forEach 中添加的单击处理程序单击的元素? - How can I get the element that is clicked on with a click handler added in a forEach?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM