【问题标题】:Event fired when text input field becomed active (not onfocus) in FireFox当文本输入字段在 FireFox 中变为活动状态(不是 onfocus)时触发的事件
【发布时间】:2014-10-18 02:06:20
【问题描述】:

我正在尝试检测文本输入字段何时处于活动状态。我最初是使用onfocus 实现的,但是,当窗口不再是前景窗口时,会触发onblur 事件。这在我的应用程序中产生了意想不到的后果。 id 是 DOMActivate 被触发的事件,but in firefox this is NOT fired on text:input fields。

文档中一次只能有一个元素处于活动状态。一个活跃的 元素不一定有焦点,但有焦点的元素是 始终是文档中的活动元素。例如,一个主动 不是前景窗口的窗口中的元素没有 重点。

触发:按钮,输入:按钮,输入:复选框,输入:文件,输入:图像,输入:密码,输入:收音机,输入:重置,输入:提交

不触发:输入:文本、选择、文本区域

我还使用input 事件进行了测试。但是,仅当您实际在框中键入时才会触发此事件。对于我的应用程序,我需要在输入任何内容之前捕获文本输入字段何时处于活动状态,并且当窗口不在前台时它无法更改状态。

我认为可能有一种方法可以使用突变观察者来执行此操作,然后检查该突变元素是否具有activeElement,但这似乎需要更多开销。我不认为有 lostActiveElement 事件,所以这也会增加一些额外的跟踪。有没有更直接/有效的方法来做到这一点?

这是一个使用插件 sdk 的 firefox 插件。

【问题讨论】:

  • 您需要监听窗口的blur 事件(或任何可能知道窗口何时不再位于前台的事件)。此外,使用文本框的 focus 和 blur 处理逻辑应该可以满足您的所有需求。例如,如果文本框获得焦点,则窗口被发送到后台,窗口的blur 事件和文本框的blur 事件将触发。但是你会忽略它,因为窗口的blur 事件发生了(这意味着文本框并没有真正模糊)。
  • 听起来很有可能,但是当项目失去“活动”时我需要取消设置。我遵循逻辑,但它需要协调多个事件。
  • 对,所以只有当窗口没有模糊,但文本框已经模糊时,你才会取消设置。不幸的是,你是对的,我很确定这是唯一的方法,除非你仔细观看activeElement
  • 在你的模糊事件顶部:if(e.target!=this||e.target!=document.activeElement){return;}
  • @dandavis 这并不完全符合我的需要。当我在我的文本字段的模糊处理程序中运行它时,如果我将窗口移动到背景,它确实会阻止触发模糊事件,但是当我点击其他地方时,模糊事件永远不会触发,就像我只点击背景一样页面,并且我不再在该字段内活动,它应该仍然模糊。

标签: javascript dom firefox-addon-sdk


【解决方案1】:

我使用mutationobever得到了可用的结果,如下所示:

var activeInputObserver = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
        if(document.activeElement.tagName === "INPUT"){     
            // do stuff based on this being the active input                
        }else{  
            // the else event is being used like a lost focus event now
            // however, it now makes it global as opposed to per input  
        }       
    });    
});

var activeInputConfig = { attributes: true, childList: true, characterData: true, subtree: true }
var activeInputTarget = document.querySelector('body'); 

if(target != null){ 
    activeInputObserver.observe(activeInputTarget, activeInputConfig);
}

我在身体上使用突变观察器并使用subtree: true 监控所有后代。这并不理想,因为现在任何时候元素激活它都会运行我的所有逻辑,而不是仅在输入字段上。它需要制作一些变量并跟踪它,而不是仅仅依赖于每个元素的事件。无法以这种方式检测元素不再处于活动状态。这种工作,但不是很理想。

我尝试在每个输入字段上放置和变异观察者,但成为活动元素不会触发变异:

var activeInputObservers = [];
inputFields = document.querySelectorAll("input");
for each (var inputField in inputFields){

    var activeInputConfig = { attributes: true, childList: true, characterData: true, subtree: true }
    var activeInputTarget = inputField;

    var thisObserver = new MutationObserver(function(mutations) {
    activeInputObservers.push(thisObserver);
        mutations.forEach(function(mutation) {
            console.log("active element:" + JSON.stringify(document.activeElement)   );         
        });    
    });

    thisObserver.observe(activeInputTarget, activeInputConfig);     
}

使用上面的代码,只需单击元素或按 Tab 键进入它不会触发可观察到的变化(在字段中键入会触发)。

activeElement 是文档的属性,因此您需要监视文档的更改;同样,您仍然无法在特定元素中捕获和事件,因此您需要在每次活动元素更改时运行一个动作。看起来输入字段本身没有任何变化。

【讨论】:

    【解决方案2】:

    遵循@dandavis 的一些建议并玩弄,我发现document.activeElement 在窗口未聚焦时不会取消设置。如果我在输入字段blur 事件中设置条件,它不会冒泡到窗口。我担心这可能会破坏其他一些脚本,当页面在后台时,窗口会接收模糊,但是,就我的插件而言,这会阻止模糊动作的发生。

    inputFields = document.querySelectorAll("input");
    for each (var inputField in inputFields){
    
        inputField.onfocus = function(){
            //do something when the input field get's focus
        }   
    
        inputField.onblur = function(e){
    
            inputStillActive = false;
            if(this === document.activeElement){ //this will be the input field element
                inputStillActive = true;
                return; //or do something else knowing that inputStillActive will be true 
            }
    
        }
    
    }
    

    与其提早返回,不如采取一些行动。如果你只做 else 或 not equal 你应该能够认为“真的失去焦点”而不是“仍然活动但窗口失去焦点”。

    在我的情况下,如果我有一个输入元素处于活动状态,我想捕获与KeePass autotype 一起使用的值,所以我将当前输入字段名称和 id 添加到窗口标题,所以简化版本类似于这个:

    var activeInputAttributes = {};
    
    inputFields = document.querySelectorAll("input");
    for each (var inputField in inputFields){
    
        inputField.onfocus = function(){
    
            activeInputAttributes["name"] = this.getAttribute("name");
            activeInputAttributes["id"] = this.getAttribute("id");
    
            console.log(activeInputAttributes);
    
        }   
    
        inputField.onblur = function(e){
    
            inputStillActive = false;
            if(this !== document.activeElement){
                activeInputAttributes["name"] = null;
                activeInputAttributes["id"] = null; 
            }
    
            console.log(activeInputAttributes);
    
        }
    
    }
    

    activeInputAttributes 仅在我实际离开输入字段时打印到 null,而当我将窗口置于后台时 not null,这意味着这些值仍然可用于自动类型.我认为这比我原来的答案更理想。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 2019-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多