开发者

Binding to key press doesn't work in IE8 and older

开发者 https://www.devze.com 2023-03-21 16:01 出处:网络
I have this code on my web page: $(document).keypress(function(e){ if (e.which == 13){ if ($(\'#title\').is(\":focus\"))

I have this code on my web page:

$(document).keypress(function(e){
    if (e.which == 13){
        if ($('#title').is(":focus"))
        {
            $("#save_post").focus().click();
            $('div .jqEasyCounterMsg').css('visibility','hidden');
        }
        else i开发者_开发问答f ($('#s').is(":focus"))
        {
            $("#searchAddress").focus().click();
        }
    }
});

This works in every single browser except older versions of IE (8 and older). What should I change to get this to work in those versions?


Place your event handler code within a $(document).ready(... block and it should work.

$(document).ready(function() {
    $(this).keypress(function() {
        ...
    });
});

See http://api.jquery.com/ready/

0

精彩评论

暂无评论...
验证码 换一张
取 消