我可以使用jQuery将函数绑定到<input>元素上下文中的keyup事件,这样该事件只在<input>元素聚焦时触发:
$('#my-input').keyup(function(e) {
// only fires when the focus is on the <input> element
}如何将函数绑定到input 元素的上下文外的密钥事件?--仅当<input>元素没有聚焦时,才能触发密钥事件:
$(':not(#my-input)').keyup(function(e) {
// this code fires regardless of whether the input element is focused
}不幸的是,不管<input>框是否聚焦,上面的函数都是运行的。
发布于 2010-08-05 03:21:24
$('#my-input').focus(function(){
$('body').unbind('keyup');
}).blur(function(){
$('body').keyup(function(e) {
//do stuff
});
});发布于 2016-05-06 17:05:06
另一种解决方案是检查事件目标是否为输入:
$('body').keyup(function(e) {
if(!$(e.target).is('input')){
console.log('i am not input');
}
});https://stackoverflow.com/questions/3411471
复制相似问题