jQuery将键绑定到主体(类除外) [英] Jquery binding key to body except a class

查看:68
本文介绍了jQuery将键绑定到主体(类除外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个键绑定到我的整个页面上,但不绑定到一类元素上.

I'm trying to bind a key to my entire page except to one class of elements.

$('*').not('.textarea-note').keypress(function (event) {
    // if key pressed is space
    if (event.which == 32) {
        alert('space pressed');
        event.preventDefault();
    }
});

问题是我需要执行preventDefault(),当我在文本区域中时,我无法制作空间角色.

The problem is that I need to do the preventDefault() and when I'm in a textarea then I can't make a space caracter.

我做错什么了吗,或者除了某类或某些东西之外,不可能绑定到所有东西.

Am I doing something wrong or it's not possible to bind to everything except some class or something.

提前谢谢!

在罗兰(Roland)发表评论后,我想出了这个效果很好的方法.

After the comment from Roland, I came up with this instead which is working perfectly.

$(document).keypress(function (event) {
    // if key pressed is space
    if (event.which == 32 && event.target.nodeName != "TEXTAREA") {
        if (videoPlaying) {
            pauseVideo();
        } else {
            playVideo();
        }
        event.preventDefault();
    }
});

推荐答案

我认为您正在寻找......

I think you are looking for this...

$(document).keypress(function(event) {
  // if key pressed is space
  if (event.which == 32) {



    if (event.target.id !== "a1") {// for class $(event.target).attr('class')
      alert('space pressed');
      event.preventDefault();
    }
  }
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<textarea id="a1"></textarea>
<textarea id="a2"></textarea>
<textarea id="a3"></textarea>

这篇关于jQuery将键绑定到主体(类除外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆