0

I am working on a project, what includes a smartsearch engine.

I'd like to write a method what makes the client write inside the smartsearch, even when it's not focused. F.e. browsing the site, the client hits down a key, and the focus jumps to the smartsearch.

That's working fine with this simple code:

$(document).ready(function()
{   
 $("*").keydown( function()
 { 
   $("input.ss-24#b").focus();
 });
});

But, yeah as You can see, it unfocuses other inputs too, and that's not the way I want it. I have tried several 'possible-solutions', like :not() and even .not() method like:

$(document).ready(function()
{   
 $("*").not("input").keydown( function()
 { 
  $("input.ss-24#b").focus();
 });
});

But it still unfocuses fields with "input" tagname too. What should I do to force jQuery not to select input fields for this event listener?

Thanks, Steven.

4

2 回答 2

2
$(document).keydown(function() {
        if (!$('input').filter(':focus').length) {

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

        }    
});

工作小提琴

于 2013-10-04T14:32:12.117 回答
0
 $(document).ready(function () {
     $(document).keydown(function (e) {
    $("input.ss-24#b").focus();
    });
});

这行得通吗?

http://jsfiddle.net/Xbbu8/

于 2013-10-04T14:31:39.880 回答