[英]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”的Unicode值为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;
}
});
顺便说一句,您在注释中写了“按A到顶部”,但是代码使用了“ t”字符代码(84); 正确的字符代码是65。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.