如何将焦点放在第一个& lt; select& gt;使用JQuery页面的元素? [英] How to set the focus on the first <select> element of the page using JQuery?

查看:102
本文介绍了如何将焦点放在第一个& lt; select& gt;使用JQuery页面的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在页面加载时使用JQuery将焦点设置在页面的第一个元素上. 我可以使用

I would like to set the focus on the first element of the page using JQuery when the page is loaded. I could successfully set the focus for first element by using

$(":input:visible:first").focus(); 

$(':input:enabled:visible:first').focus();

根据我尝试使用以下任何一种方法:

I tried to use any of the below:

$('select:first').focus();
$("select").first().focus();
$(':visible:first').focus();
$('enabled:visible:first').focus();

我可能想到的

或其他变体,但没有成功.

or other variations I could think of, but have not succeeded.

有人可以帮助我解决这个问题吗? 我是JQuery的初学者.

Could anyone help me on this problem? I am a beginner to JQuery.

我希望此功能适用于所有主流浏览器.

I would like this function to work for all major browsers.

非常感谢!

推荐答案

问题很可能是因为在加载DOM之后您没有在运行代码.试试这个:

The problem is most likely to be that you are not running the code on after the DOM is loaded. Try this one:

$(document).ready(function(){
   $("select:first").focus();
});

应该可以.

编辑:您可以使用以下方法简化代码:

You can simplify the code a bit by using this:

$(function(){
  $("select:first").focus();
})

如果您以这种方式使用它,那么当文档的DOM准备就绪时,jQuery将触发该函数.

If you use it that way, jQuery will trigger that function when the document's DOM is ready.

这篇关于如何将焦点放在第一个& lt; select& gt;使用JQuery页面的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆