onclick() and onblur() ordering issue

前端 未结 6 1934
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 14:52

I have an input field that brings up a custom drop-down menu. I would like the following functionality:

  • When the user clicks anywhere outside the input field,
6条回答
  •  一个人的身影
    2020-12-04 15:32

    A more elegant (but likely less performant) solution:

    Instead of using the input's onblur to remove the menu, use document.onclick, which fires after onblur.

    However, this also means that the menu is removed when the input itself is clicked on, which is undesired behaviour. Set an input.onclick with event.stopPropagation() to avoid propagating clicks to the document click event.

提交回复
热议问题