将焦点设置在按钮上不起作用
我试图在用户按文本框中的 Enter 键时将焦点设置到按钮。但它不起作用。我使用的是 Internet Explorer 8 浏览器。我错过了什么吗?
$("input.Box").live('keydown', function(e) {
if (e.keyCode == 13) {
e.preventDefault();
$("#button").focus(); // Not working?
}
});
I am trying to set the focus to a button while the user presses the Enter key in the text box. But it is not working. I am using the Internet Explorer 8 browser. Am I missing something?
$("input.Box").live('keydown', function(e) {
if (e.keyCode == 13) {
e.preventDefault();
$("#button").focus(); // Not working?
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是 IE 的响应速度不够快,因此您需要在输入
live
函数和.focus()
之间添加一个小的延迟。叫。因此,请将$("#button").focus();
替换为
This,并将
e.which
与e.keyCode
结合使用正如 Blender 建议的那样应该可以解决你的问题。The problem is that IE is not able to respond quickly enough, so you need to add a small delay between when the
live
function is entered, and when.focus()
is called. So, replace$("#button").focus();
with
This, in conjunction with using
e.which
withe.keyCode
as Blender suggested should fix your issue.Microsoft 决定不喜欢
e.keyCode
,而是采用自己的语法e.which
。您必须检查两者:
Microsoft decided that they don't like
e.keyCode
and instead have their own syntax,e.which
.You have to check for both:
你确定名字正确吗? .NET 有重命名事物的习惯。您不指定语言或环境。
尝试使用类选择器。为按钮指定类名称
class="Test"
,然后使用$(".Text").focus()
进行选择。Are you sure the name is correct? .NET has a habit of renaming things. You don't specify the language or environment.
Try to use the class selector. Give the button a class name of
class="Test"
and then select using$(".Text").focus()
.在尝试设置焦点之前,请确保 DOM 已准备好、元素存在。
Make sure the DOM is ready, element exists, before attempting to set focus.