JavaScript FocusOut - get the element that receives focus

淺唱寂寞╮ 提交于 2019-12-22 03:50:33

问题


When the FocusOut event is raised, how do you know which element receives the focus?

The correct way seems to be to use the event's relatedTarget property. However, this seems not to work in all browsers:

  • in Google Chrome, it works
  • in Firefox and Internet Explorer, the relatedTarget is null
  • in Safari, the relatedTarget property doesn't even exist

I have found a workaround which works only in IE (using document.activeElement), but I'm wondering if there isn't a general solution that has proven to work in all major browsers.

Although I can find similar questions and answers, I haven't found any solution which really works in all browsers.

EDIT: the example below shows what I mean.

Code:

document.getElementById('text1').addEventListener('focusout', function(e) {
  // At this point, I want to know which element is receiving the focus (i.e. text2)
  console.log(e.relatedTarget); // works in Chrome
  console.log(document.activeElement); // works in IE
  // both do not work in Firefox or Safari
});
<input id="text1" type="text" value="first" />
<input id="text2" type="text" value="second" />

回答1:


I have a hypothesis and workaround for firefox. document.activeElement seems to work. Then focusout hits, so it gets removed. By the time focusin hits (or maybe immediately after) there will be a focused element again. But between out and in there is nothing focused, so no element is being reported as active.

My workaround is a stupid setTimeout hack. setTimeout( function() {console.log(document.activeElement)}, 1); reliably gets me an active element. Granted I've only tested in one machine and spent all of 90 seconds doing so, but it's the best I've found so far.




回答2:


I believe what you're looking for is document.activeElement.

Returns the currently focused element, that is, the element that will get keystroke events if the user types any. This attribute is read only.

https://developer.mozilla.org/en-US/docs/Web/API/document.activeElement




回答3:


I have encounter the same problem using textEditor PrimeFaces element.

I use the following html code.

<div contenteditable="true" class="div-focus"> 
    <h:outputText
        styleClass="OutputField" 
        value="#{oController.panelTpl.getComment()}"  
        escape="false"
        />
</div>    
<div class="text-editor"> 
    <p:textEditor 
       value="#{oController.panelTpl.txtComment.text}"
       />
</div>    

The TextArea is defined twice so that when form is displayed, the user see only the first <div> widget without seeing a specific toolbar.

When user click on text displayed, the focusin() event hide the <div class="div-focus"> element and display the <p:textEditor> widget contained in <div class="text-editor"> parent element.

To do that, I have defined the following javascript code

function onLoadDialog()
    {
    jQuery(".text-editor").hide();

    jQuery(".div-focus").focusin(function()
        {
        $(this).hide();
        $(this).next(".text-editor").show();
        $(this).next(".text-editor").focus();
        });            

    jQuery(".text-editor").focusout(function(e)
        {
        if ($(e.relatedTarget).closest(".text-editor").size() == 0)
            {
            $(this).hide();
            $(this).prev(".div-focus").show();
            $(this).prev(".div-focus").text($(this).text());
            }
        });            
    }

The onLoadDialog function() is called when page is loaded and is used to hide <div class="text-editor> element and to defined focusin() and focusout() events.

The focusin() event hide <div class="div-focus"> element and display <div class="text-editor"> element. When the user clicks on text in <div class="div-focus"> element, this element is hidden and the following hidden element is display. The <div class="text-editor"> element gains focus.

The focusout() event hide <div class="text-editor"> element and display <div class="div-focus"> element ... only when element that gains focus is not defines in <div class="text-editor"> element.

The problem with element.focusout() is that it is triggered each time an element included in main element even if element that gains focus is defined in same element.

When you enter in a house, you can enter passing by garage or living room or kitchen. When you enter in living room (by external door), you enter in the building. But when you quit living room to kitchen, you stay in house !!! If you quit living room to go in garden, you quit the house (building).

focusin() function implement the same principe, but not focusout() !

if ($(e.relatedTarget).closest(".text-editor").size() == 0) line used in focusout() event correct this little difference !

CAUTION: the solution is not perfect because I have some little problems to solve when copying text from textEditor widget to outputText widget without formatting !




回答4:


//attach a focus out handler
$("#id").focusOut($El_FocusOut);

//display the id of new element being focused onto
function $El_FocusOut($eventArgs){
    //firefox specific focusout active element logi
    var activeElement = $eventArgs.originalEvent.explicitOriginalTarget;
    alert($(activeElement).attr("id"))
}


来源:https://stackoverflow.com/questions/22879572/javascript-focusout-get-the-element-that-receives-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!