捕获回车键的跨浏览器,我的解决方案不工作 [英] Capture the enter key cross-browser, my solution isn't working

查看:185
本文介绍了捕获回车键的跨浏览器,我的解决方案不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是一个跨浏览器的解决方案,捕获我在聊天脚本中输入密钥,这里是:

I have what I thought was a cross-browser solution to capturing the enter key in a chat script I'm making, here it is:

    nn=(document.layers)?true:false;
ie=(document.all)?true:false;
function keyDown(e) {
    var evt=(e)?e:(window.event)?window.event:null;
    if(evt){
        var key=(evt.charCode)?evt.charCode: ((evt.keyCode)?evt.keyCode:((evt.which)?evt.which:0));
        if(key=="13") document.getElementById('chatEnter').submit();
        }
    }
document.onkeydown=keyDown;
if(nn) document.captureEvents(Event.KEYDOWN);

我是从别人那里得到的,所以也许已经过时了?无论如何,窗体id属性是chatEnter,你可以看到。我也试过使用document.forms [0] .submit,也没有工作。它工作正常在FF,但没有运气在IE8 64位(这些是我迄今为止测试的唯一两个。)我在这里做错了什么?非常感谢您的帮助。

I got this from someone else so perhaps it's outdated? Anyway, the form id attribute is chatEnter as you can see. I've also tried using document.forms[0].submit and that didn't work either. It works just fine in FF but no luck in IE8 64 bit (those are the only two I've tested on thus far.) What am I doing wrong here? Thanks for any help.

推荐答案

请尝试使用>

$(document).keypress(function(e) {
    if (e.which == "13") { 
        //enter pressed 
    }       
});

当然,你需要jQuery。与使用经过测试并广泛使用的跨浏览器框架相比,不能有更好的跨浏览器解决方案。

ofcourse you would require jQuery for it. There can't be a better cross-browser solution than to use a tested and widely used cross browser framework.

这篇关于捕获回车键的跨浏览器,我的解决方案不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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