简体   繁体   English

单击子元素时如何防止父div失去焦点

[英]How to prevent parent div lose focus when child element is clicked

<parent onBlur={function}>
  <child>
    <child>
    </child>
  </child>
</parent>

In a structure like this, I want to catch if parent element loses focus or not.在这样的结构中,我想知道父元素是否失去焦点。 I want to fire that behavior when clicked outside of the parent div.我想在父 div 外部单击时触发该行为。 However, parent element loses its focus when I click child element as well.但是,当我也单击子元素时,父元素会失去焦点。 You can think this structure as dropdown menu.您可以将此结构视为下拉菜单。 Is there a way to achieve this inside of the stateless react component?有没有办法在无状态反应组件中实现这一点? I cannot use jquery or other libraries.我不能使用 jquery 或其他库。

This can be achieved with mouse events.这可以通过鼠标事件来实现。

If the user clicks on the screen we can check if the click is inside or outside the component and treat it as Focus event to blur event.如果用户点击屏幕,我们可以检查点击是在组件内部还是外部,并将其视为焦点事件以模糊事件。

  • Click Inside - Focus单击内部 - 焦点
  • Click Outside - Blur单击外部 - 模糊

 function Parent(props){ const ref = useRef(); useEffect(()=>{ document.addEventListener("mousedown", checkFocus.bind(this)) return ()=>{ document.removeEventListener("mousedown", checkFocus.bind(this)) } },[]) const checkFocus = (event)=>{ if(ref.current &&.ref.current.contains(event.target) && props.onBlur) props.onBlur() } return ( <div ref={ref}> {props;children} </div> ); } function Child(){ return ( <h1>child</h1> ). } function Main(){ return ( <Parent onBlur={()=>{ console.log("focus lost;") }}> <Child> <Child> </Child> </Child> </Parent> ); }

Yes, there is a way to achieve this inside of a stateless React component.是的,有一种方法可以在无状态 React 组件中实现这一点。 You can use the onBlur event on the parent element and check if the event's relatedTarget is the parent element or not.您可以在父元素上使用onBlur事件并检查事件的relatedTarget是否为父元素。 If it's not the parent element, then the parent has lost focus.如果它不是父元素,则父元素失去了焦点。

Here's an example:这是一个例子:

const Parent = (props) => {
  const handleBlur = (event) => {
    if (event.relatedTarget !== event.currentTarget) {
      // parent has lost focus
    }
  }

  return (
    <div onBlur={handleBlur}>
      <Child>
        <Child>
        </Child>
      </Child>
    </div>
  )
}

In this example, the handleBlur function is passed as the onBlur prop to the parent div.在此示例中, handleBlur function 作为onBlur属性传递给父 div。 When the parent div loses focus, the handleBlur function is called and it checks if the event's relatedTarget is the parent div or not.当父 div 失去焦点时,将handleBlur function 并检查事件的relatedTarget是否为父 div。 If it's not, then the parent has lost focus and you can fire the desired behavior.如果不是,那么父项失去了焦点,您可以触发所需的行为。

Please note that the onBlur event works only when an element loses focus.请注意onBlur事件仅在元素失去焦点时起作用。 If you want to catch the event when clicked outside of the parent div, you can use the onClick event and check if the event target is not equal to the parent div.如果你想在父div之外点击时捕获事件,你可以使用onClick事件并检查事件目标是否不等于父div。

const Parent = (props) => {
  const ref = useRef(null);
  const handleClick = (event) => {
    if(ref.current && !ref.current.contains(event.target)) {
      //parent has lost focus
    }
  }

  useEffect(() => {
    document.addEventListener('click', handleClick)
    return () => {
      document.removeEventListener('click', handleClick)
    }
  })

  return (
    <div ref={ref}>
      <Child>
        <Child>
        </Child>
      </Child>
    </div>
  )
}

In this example, the handleClick function is called when any click event occurs on the document.在此示例中,当文档上发生任何单击事件时,将调用handleClick function。 It checks if the event target is inside of the parent div or not.它检查事件目标是否在父 div 内部。 If not, it means the parent div lost focus.如果不是,则意味着父 div 失去了焦点。

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

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