动态添加tabindex [英] Adding tabindex dynamically
问题描述
我想将 tabindex
添加到所有表单元素.表单是动态的,我无法将其添加到 HTML 中.我想将其作为函数运行.
I'd like to add tabindex
to all form elements. The form is dynamic and I can't add it to the HTML. I would like to run it as a function.
如果有多个同名的单选按钮,每个按钮都必须有自己的 tabindex
值.页面上的大多数表单元素都以 开头,除了
.我该如何解释?
If there are several radio buttons with the same name, each must have it's own tabindex
value. Most of the form elements on page start as <input>
, except <select>
. How do I account for that?
我想我需要运行一个循环并添加属性,对吗?
I guess I will need to run a loop and add the attribute, right?
var n = 1;
$('input, select').each(function() {
$(this).attr('tabindex', n++);
});
推荐答案
奇怪的问题,不过基本思路是这样的:
Strange question, but yes that's the basic idea:
$(":input:not(:hidden)").each(function (i) { $(this).attr('tabindex', i + 1); });
这使用 :input 来获取包括按钮和文本区域在内的所有内容.:not(:hidden)
只会排除隐藏的输入以避免不必要的选项卡.
This uses :input to get everything including buttons and text areas. :not(:hidden)
will just exclude the hidden inputs to avoid unnecessary tabs.
这篇关于动态添加tabindex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!