开发者

jQuery - pull navigational focus to a particular link on page load

As soon as a page loads, I want t开发者_如何学Che focus to jump to a particular link, so hitting enter will 'click' it.

I've tried

$('ul li a:first').focus();

without luck, but I think I may be misinterpreting the usage of .focus().

Any advice appreciated!

:s


Your code is almost right. You just miss $(document).ready.

To fix, use this:

$(document).ready(function(){
   $('ul li a:first').focus();
});

See example in jsfiddle.

The problem is when jquery is called the page doesn't have that link yet. With $(document).ready. you call jquery only when page is complete.


Yeah, though it's a little odd how it works (and I found this on the jQuery focus docs, and don't claim to understand why it works):

$(document).ready(
  function() {
    $("a:first").attr("tabindex",'-1').focus();
  }
  );

Demo at: jsbin

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜