Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要一些关于 jquery 选择器的帮助...
我需要在按键操作上调用一个函数,但只有当用户不在<form>.
<form>
我尝试$('body').not('form').keypress(getKey)但它不起作用
$('body').not('form').keypress(getKey)
我尝试了一些.not()选择器的组合,但我没有做任何事情......
.not()
因此,如果有人知道我该怎么做...
谢谢
$(document).on('keypress', function (e) { if (!$(e.target).closest('form').length) { //not in a form } });
http://jsfiddle.net/ExplosionPIlls/ZfAdk/
$('*:not(form *)').bind('keypress', function (e) { // not in a form });
http://jsfiddle.net/zmc2z/