【问题标题】:Execute function only when element is not focused [duplicate]仅在元素未聚焦时执行功能[重复]
【发布时间】:2017-04-27 08:49:05
【问题描述】:

我的网站上有一个脚本正在运行,但我希望它仅在某些元素未聚焦时执行。这是我的大致想法

window.setInterval(function(){
   if (document.getElementById('someElement') != focused) {
   doSomething;
   }
}, 1000); 

“someElement”设置为可编辑,因此可以单击(聚焦)。

我的问题是“!= focus”在 javascript 中的外观。

【问题讨论】:

    标签: javascript html function select element


    【解决方案1】:

    你可以使用document.activeElement

    喜欢这个

    if (document.getElementById('someElement') !== document.activeElement)
    

    【讨论】:

      【解决方案2】:

      使用它来检查焦点......

      window.setInterval(function(){
         if (document.getElementById('someElement').is(":focus") {
         doSomething;
         }
      }, 1000); 
      

      【讨论】:

      • 它只适用于 jquery
      猜你喜欢
      • 2014-07-01
      • 1970-01-01
      • 2019-10-23
      • 2015-10-07
      • 2013-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多