开发者

jquery dropdown focus

I am trying to set a focus on a dropdown within GridView(gridViewDropDown class) when page loads:

if ($('select.gridViewDropDown').length)
{
     alert("Found my dropdown");
        //$('select.gridViewDropDown:first').focus();
     setTimeout(function() { $('select.gridViewDropDown').focus(); }, 10);
}

I can see the alert which means that dropdown is found but it never gets a focus. What do I need开发者_如何学Python to change here? I am using IE 6/7.


I'd try this approach:

$(function() {
  setTimeout(function() { $('select.gridViewDropDown').focus(); }, 50);
});

You need to wait until the DOM is ready, it's possible that something else is set to steal focus when the page loads (code that's most likely executing when the DOM's ready as well) This approach times it to be just slightly after that happens.

It also takes advantage of how jQuery works, if no elements are found when this function fires, none will steal focus, so it's safe to just leave in there as-is.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜