动态添加tabindex [英] Adding tabindex dynamically

查看:64
本文介绍了动态添加tabindex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将tabindex添加到所有表单元素.表单是动态的,我无法将其添加到HTML中.我想将其作为一个函数来运行.如果有多个具有相同名称的单选按钮,则每个按钮必须具有其自己的tabindex值.除SELECT之外,页面上的大多数表单元素均以INPUT开头.我该如何处理?

I'd like to add tabindex to all form elements. The form is dymanic and I can't add it to the HTML. I would like to run it as a function. If there are several radiobuttons 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 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屋!

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