2016-03-07 40 views

回答

0

jQuery和制表指数

方式tabindexing工作原理是,它进入低谷的HTML标记并寻找tabable元素。

所以说,你有这样的代码:

<form> 
 
    <label>Name:</label> 
 
    <input type="text" /> 
 
    <br> 
 
    <input type="submit" /> 
 
</form>

文本输入将首先标签,然后提交输入。
是的,我知道这是如何得到标签?

$(function() { 
 
    $firstinput = $("form").find("input") 
 
    console.log($firstinput.first()); //prints the first element thats an input. 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form> 
 
    <label>Name:</label> 
 
    <input type="text" /> 
 
    <br> 
 
    <input type="submit" /> 
 
</form>
上面的代码,如果你只需要输入elemtns,而不是挂羊头卖狗肉添加到标记像使用jQuery UI :focusable选择隐藏你的元素等

在下面IM代码只会工作。 有了这个,你应该得到的第一tabbable元素的形式

//:focus selector from jquery UI 
 
$.extend($.expr[':'], { 
 
    focusable: function(element) { 
 
    var nodeName = element.nodeName.toLowerCase(), 
 
     tabIndex = $.attr(element, 'tabindex'); 
 
    return (/input|select|textarea|button|object/.test(nodeName) ? !element.disabled : 'a' == nodeName || 'area' == nodeName ? element.href || !isNaN(tabIndex) : !isNaN(tabIndex)) 
 
     // the element and all of its ancestors must be visible 
 
     // the browser may report that the area is hidden 
 
     && !$(element)['area' == nodeName ? 'parents' : 'closest'](':hidden').length; 
 
    } 
 
}); 
 

 
//actual code 
 
$(function() { 
 
    $form = $("form"); 
 
    $first = $form.find(":focusable").first(); 
 
    console.log($first); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form> 
 
    <label tab-index="0">Name:</label> 
 
    <input type="text" /> 
 
    <br> 
 
    <input type="submit" /> 
 
</form>

0

您可以搜索 '的TabIndex' 使用jQuery属性。

var tabindexVal = $('selector').attr('tabindex');

+0

我想找到的时候我不会放弃任何一个选项卡索引后返回的默认选项卡索引。 –

+0

我认为tabindex零意味着使用tabindex显示顺序。所以你可以测试jQuery的选择顺序输入,如$('input')。each(function(index){console.log(index);});'' –