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

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

问题描述

我想在加载页面时将焦点放在使用JQuery的页面的第一个元素上。
我可以通过使用

  $(:input:visible:first)成功设置第一个元素的焦点。焦点(); 

  $( ':输入:启用:可见:第一')聚焦(); 

根据 jquery,将焦点设置在页面上的第一个启用的输入或select或textarea上



我尝试使用以下任何一项:

  $( '选择:第一')聚焦(); 
$(select)。first()。focus();
$(':visible:first')。focus();
$('enabled:visible:first')。focus();

或其他我可以想到的变化,但没有成功。

任何人都可以帮我解决这个问题吗?
我是JQuery的初学者。



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




解决方案

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

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

这应该可以。

编辑:你可以使用这个简化代码:

  $(function(){
$(select:first)。focus();
})

如果你这样使用它,jQuery将在文档的DOM准备就绪时触发该函数。


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(); 

or

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

according to jquery, set focus on the first enabled input or select or textarea on the page.

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.

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

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

Thank you very much!

解决方案

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();
});

That should work.

EDIT: You can simplify the code a bit by using this:

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

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

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

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