将焦点设置在按钮上不起作用 [英] Setting focus on a button is not working

查看:94
本文介绍了将焦点设置在按钮上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在用户按下文本框中的 Enter 键时将焦点设置为按钮.但这是行不通的.我正在使用Internet Explorer 8浏览器.我想念什么吗?

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?
    }
});

推荐答案

Microsoft决定他们不喜欢e.keyCode,而是使用自己的语法e.which.

Microsoft decided that they don't like e.keyCode and instead have their own syntax, e.which.

您必须同时检查两者:

$("input.Box").live('keydown', function(e) {
    var keyCode = (window.event) ? e.which : e.keyCode;

    if (keyCode == 13)
        e.preventDefault(); 
        $("#button").focus(); // Not working?
    }
});

这篇关于将焦点设置在按钮上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆