开发者

Jquery - Set focus to window

开发者 https://www.devze.com 2023-04-02 11:36 出处:网络
any ideas why this not work? $(document).ready(function ubsrt() { $(window).bind(\'keyup\', function(e) { if (e.keyCode == \'27\')

any ideas why this not work?

$(document).ready(function ubsrt()
{
    $(window).bind('keyup', function(e) { if (e.keyCode == '27') 
    { 
        $('body').append('focus window <br />');
        $(window).focus(); 
        $开发者_运维问答(document).focus(); 
    } });

   $('#test').focus();

});

example

http://jsbin.com/agayen/edit#preview


I think what you're trying to do is to remove focus from the text box when you hit escape, so try this (in your event handler):

$(e.target).blur();

In this example focus never leaves from the window so you can't assign it back.

You could test to see if the target is valid for a blur call too - e.g. test if it's an input.


Yes, you don't need to give the function a name. There you are defining a function, not calling one:

Instead of:

$(document).ready(function ubsrt()
{

Use:

$(function ()
{
0

精彩评论

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