禁用文本区域和输入中的快捷方式
Disable shortcut in textarea and input
我有这个 javascript 允许我放置快捷方式,但我想在文本区域和输入中停用它们
//press A for back to top
jQuery(document).keydown(function(e){
var target = e.target || e.srcElement;
if ( target.tagName !== "TEXTAREA" || target.tagName === "INPUT" ) {
if(e.which == 84) {
$("html, body").animate({ scrollTop: 0 }, 500);
return false;
}
}
});
但是因为如果我插入 ( target.tagName !== "TEXTAREA" || target.tagName === "INPUT" )
脚本不起作用?我该如何解决?
你条件有误,正确的是:
if ( target.tagName != "TEXTAREA" && target.tagName != "INPUT" ) {...}
此外 'A' 的 Unicode 值是 65,'T' 是 84。
您可以将代码简化为
//press A for back to top
$(document).keydown(function(e){
console.log(e);
if(e.which == 65 && e.target.tagName !== 'INPUT'&& e.target.tagName !== 'TEXTAREA') {
$("html, body").stop().animate({ scrollTop: 0 }, 500);
return false;
}
});
顺便说一下,您在评论中写了 "press A to top",但您的代码使用了 "t" charcode (84);正确的字符代码是 65.
我有这个 javascript 允许我放置快捷方式,但我想在文本区域和输入中停用它们
//press A for back to top
jQuery(document).keydown(function(e){
var target = e.target || e.srcElement;
if ( target.tagName !== "TEXTAREA" || target.tagName === "INPUT" ) {
if(e.which == 84) {
$("html, body").animate({ scrollTop: 0 }, 500);
return false;
}
}
});
但是因为如果我插入 ( target.tagName !== "TEXTAREA" || target.tagName === "INPUT" )
脚本不起作用?我该如何解决?
你条件有误,正确的是:
if ( target.tagName != "TEXTAREA" && target.tagName != "INPUT" ) {...}
此外 'A' 的 Unicode 值是 65,'T' 是 84。
您可以将代码简化为
//press A for back to top
$(document).keydown(function(e){
console.log(e);
if(e.which == 65 && e.target.tagName !== 'INPUT'&& e.target.tagName !== 'TEXTAREA') {
$("html, body").stop().animate({ scrollTop: 0 }, 500);
return false;
}
});
顺便说一下,您在评论中写了 "press A to top",但您的代码使用了 "t" charcode (84);正确的字符代码是 65.