Blur event stops click event from working?

后端 未结 6 817
野的像风
野的像风 2020-12-02 16:49

It appears that the Blur event stops the click event handler from working? I have a combo box where the options only appear when the text field has focus. Choosing an option

相关标签:
6条回答
  • 2020-12-02 17:06

    I know this is a later reply, but I had this same issue, and a lot of these solutions didn't really work in my scenario. mousedown is not functional with forms, it can cause the enter key functionality to change on the submit button. Instead, you can set a variable _mouseclick true in the mousedown, check it in the blur, and preventDefault() if it's true. Then, in the mouseup set the variable false. I did not see issues with this, unless someone can think of any.

    0 讨论(0)
  • 2020-12-02 17:08

    Performing an action that should happen on a click on a mousedown is bad UX. Instead, what's a click effectively made up of? A mousedown and a mouseup.

    Therefore, stop the propagation of the mousedown event in the mousedown handler, and perform the action in the mouseup handler.

    An example in ReactJS:

    <a onMouseDown={e => e.preventDefault()}
       onMouseUp={() => alert("CLICK")}>
       Click me!
    </a>
    
    0 讨论(0)
  • 2020-12-02 17:14

    If this.menuTarget.classList.add("hidden") is the blur behavior that hides the clickable menu, then I succeeded by waiting 100ms before invoking it.

    setTimeout(() => {
      this.menuTarget.classList.add()
    }, 100)
    

    This allowed the click event to be processed upon the menuTarget DOM before it was hidden.

    0 讨论(0)
  • 2020-12-02 17:15

    You could try the mousedown event instead of click.

    $('.ShippingGroupLinkList').live("mousedown", function(e) {
        alert('You wont see me if your cursor was in the text box');
    });
    

    This is clearly not the best solution as a mousedown event is not achieved the same way for the user than a click event. Unfortunately, the blur event will cancel out mouseup events as well.

    0 讨论(0)
  • 2020-12-02 17:17

    click event triggers after the blur so the link gets hidden. Instead of click use mousedown it will work.

    $('.ShippingGroupLinkList').live("mousedown", function(e) {
        alert('You wont see me if your cursor was in the text box');
    });
    

    Other alternative is to have some delay before you hide the links on blur event. Its upto you which approach to go for.

    Demo

    0 讨论(0)
  • 2020-12-02 17:19

    4.The best solution would be something like:

    $('#ShippingGroup').blur(function()
    {
       if($(document.activeElement) == $('.ShippingGroupLinkList'))
          return; // The element that now has focus is a link, do nothing
       $('#ShippingGroupListWrapper').css('display','none'); // hide it.
    }
    

    Unfortunately, $(document.activeElement) seems to always return the body element, not the one that was clicked. But maybe if there was a reliable way to know either 1. which element now has focus or two, which element caused the blur (not which element is blurring) from within the blur handler.

    What you may be looking for is e.relatedTarget. So when clicking the link, e.relatedTarget should get populated with the link element, so in your blur handler, you can choose not to hide the container if the element clicked is within the container (or compare it directly with the link):

    $('#ShippingGroup').blur(function(e)
    {
       if(!e.relatedTarget || !e.currentTarget.contains(e.relatedTarget)) {
       // Alt: (!e.relatedTarget || $(e.relatedTarget) == $('.ShippingGroupLinkList'))
    
           $('#ShippingGroupListWrapper').css('display','none'); // hide it.
       }
    }
    

    (relatedTarget may not be supported in older browsers for blur events, but it appears to work in latest Chrome, Firefox, and Safari)

    0 讨论(0)
提交回复
热议问题