Javascript jquery 设置 tabindex 和光标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6786115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery set tabindex and cursor
提问by ShaneKm
I have the following code that assigns tabindex to my form id "register1". I would like to place the cursor on the first input or select list item on the form (item with tabindex = 1) once tabindexes are assigned. but the following line: $('#register1').find('input').attr('tabindex',1).select();
Resets tabindex of all the inputs.
我有以下代码将 tabindex 分配给我的表单 ID“register1”。一旦分配了 tabindexes,我想将光标放在表单上的第一个输入或选择列表项(tabindex = 1 的项)上。但是下面这行:$('#register1').find('input').attr('tabindex',1).select();
重置所有输入的 tabindex。
Full code:
完整代码:
$(function(){
var tabindex = 1;
$('#register1').find('input,select').each(function() {
if (this.type != "hidden") {
var $input = $(this);
$input.attr("tabindex", tabindex);
tabindex++;
}
});
$('#register1').find('input').attr('tabindex',1).select();
});
thanks
谢谢
回答by ChristopheCVB
Try :
尝试 :
$('#register1').find('input[tabindex=1]').whatyouwant()
回答by Ibu
Simply select the item with tabindex one in your loop using a condition:
只需使用条件在循环中选择带有 tabindex 的项目:
$(function(){
var tabindex = 1;
$('#register1').find('input,select').each(function() {
if (this.type != "hidden") {
var $input = $(this);
$input.attr("tabindex", tabindex);
// select the first one.
if (tabindex == 1) {
$input.select();
}
tabindex++;
}
});
});