将tabindex添加到动态元素

时间:2013-07-25 19:19:03

标签: jquery

我有一个带有字段的表单,其中一些字段可能被隐藏。为了正确访问,我使用jQuery添加tabindex,仅添加到当前可见的元素:

$(':input:visible').each(function (i) {
    $(this).attr('tabindex', i + 1);
});

效果很好。但是,当我决定将tabindex添加到具有特定类名的span时,将跳过该元素。为什么呢?

$(':input:visible, .tabIn').each(function (i) {
    $(this).attr('tabindex', i + 1);
});

<span class="tabIn">my span</span>

1 个答案:

答案 0 :(得分:0)

这对我来说是正确的:

<html>
    <head>
        <script type="text/javascript" src="jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function () {
            $(':input:visible, .tabIn').each(function (i) {
                $(this).css('background-color','red').attr('tabindex', i + 1);
            });
        });
        </script>
    </head>
    <body>
    <span class="tabIn">my span</span>

    <input name="tabIn" />
    </body>
</html>
相关问题