简体   繁体   English

如何使用jquery为具有相似类的网页上的元素设置tabindex

[英]How to set tabindex for elements on webpage with similar class using jquery

I have multiple elements with anchor tag as below, I am trying to set tabindex only to elements with the anchor tag. 我有多个带有锚标记的元素,如下所示,我试图仅将tabindex设置为带有锚标记的元素。 Below is the code: 下面是代码:

<ul class="level1 static" tabindex="0" role="menu" style="position: relative; width: auto; float: left;">

        <li role="menuitem" class="static" style="position: relative;">
        <a class="level1 static" >program details<span class="visuallyhidden">(Current page)</span></a>
</ul>

Thanks in Advance! 提前致谢!

Simple, with $("a").attr('tabindex', 0); 简单, $("a").attr('tabindex', 0); you will add/set tabindex="0" to all a element. 您将添加/套tabindex="0"到所有a元素。

(NOTE: make sure the above code run after all your <a> element are loaded, for dynamic added <a> element just call it again after the new <a> element loaded.) (注意:确保上面的代码在所有<a>元素加载后运行,对于动态添加的<a>元素,只需在新的<a>元素加载后再次调用它即可。)

tabindex tabindex属性

The tabindex attribute is versatile, and it has the capacity to improve or destroy usability for keyboard-only users. tabindex属性是通用的,它具有改善或破坏仅键盘用户的可用性的能力。 When you think about using the tabindex attribute, keep these things in mind: 考虑使用tabindex属性时,请记住以下几点:

Use tabindex=0 to include an element in the natural tab order of the content, but remember that an element that is focusable by default may be an easier option than a custom control 使用tabindex = 0可以按照内容的自然制表符顺序包含元素,但是请记住,默认情况下可聚焦的元素可能比自定义控件更容易选择

Use tabindex=-1 to give an element programmatic focus, but exclude it from the tab order of the content 使用tabindex = -1赋予元素程序性焦点,但将其从内容的选项卡顺序中排除

Avoid using tabindex=1+. 避免使用tabindex = 1 +。

REF: https://www.paciellogroup.com/blog/2014/08/using-the-tabindex-attribute/ 参考: https : //www.paciellogroup.com/blog/2014/08/using-the-tabindex-attribute/

 //$("a").attr('tabindex', 0); $("li.static").each(function(index, elm) { $($(elm).find('a')).attr('tabindex', 0); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="level1 static" tabindex="0" role="menu" style="position: relative; width: auto; float: left;"> <li role="menuitem" class="static" style="position: relative;"> <a class="level1 static">program details<span class="visuallyhidden">(Current page)1</span></a><br> </li> <li role="menuitem" class="static" style="position: relative;"> <a class="level1 static">program details<span class="visuallyhidden">(Current page)2</span></a><br> </li> <li role="menuitem" class="static" style="position: relative;"> <a class="level1 static">program details<span class="visuallyhidden">(Current page)3</span></a><br> </li> </ul> 

Can use attr(attributeName, function) 可以使用attr(attributeName, function)

$("a").attr('tabindex',function(i) {
  return  i + 1;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM