javascript - 如果有任何输入获得焦点,则不执行函数

标签 javascript jquery focus onfocus

我正在寻找一种方法,如果页面的任何输入处于焦点状态,则不执行函数(e)。 尝试了好几种方法都没有成功;(

这是我的代码:

document.onkeyup=function(e){

  var e = e || window.event;//Pour IE   

  // Action sur la barre d'espace (32)
  if(e.which == 32) {
    $("#play").click(); 
    return false;
  }

   //Action sur entrée (13)
  if(e.which == 13) {
    $("#play").click(); 
    return false;
  }

}

我尝试过这样的事情

if ($('input').is(":focus")) {
}
else{ my code }

但是这不起作用。

谢谢你帮助我!

艾蒂安

最佳答案

javascript 中的 onfocus 事件

<!DOCTYPE html>
<html>
<body>

Enter your name: <input type="text" onfocus="myFunction(this)">

<p>When the input field gets focus, a function is triggered which changes the background-color.</p>

<script>
function myFunction(x) {
    x.style.background = "yellow";
}
</script>

</body>
</html>

关于javascript - 如果有任何输入获得焦点,则不执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49148899/

相关文章:

javascript - ('eventName' , function(){...}); 上的这个模式 : . 的名称是什么?

JavaScript 无法找到该元素

javascript - 更改按钮单击 codeigniter 中的枚举值

android - 在 EditText 上设置焦点

javascript - 根据另一个 PartialView 上的 DatePicker 验证来自 DropDown 的输入数据

jquery - 如何在jquery中选择textarea的class或id?

jquery - Kendo UI MVC 网格 DataSourceRequest JavaScript 发布附加数据

android - ListView 中的焦点控制

Java - JPanel 和 KeyListener 问题

php - 如果存在页面重定向,成功函数中的 jQuery ajax 函数将被忽略