admin管理员组文章数量:1333437
In the accepted answer to this question Multiple submit buttons in an HTML form the ment is raised:
Please don't do this without also changing the tab order, so that hitting the tab button will cycle through the buttons as they appear on screen.
My question is: is there a way to set the tabindex on those two buttons to acplish this ordering without having to assign a specific tabindex to every other tabable element on the page?
My understanding of tabindex values is that specified positive values proceed elements w/o a specified value, so I am at a loss to figure out a way to do this w/o going through and assigning everything else a value.
If indeed, assigning a specific tabindex to every item is the only way, is there some (hopefully short and hopefully jQuery) magic to assign every appropriate element on the page a tabindex of, say, 1?
EDIT
As it looks like the solution is going to involve applying a specific tabindex to every other tabable object -- it seems like an important part of the solution is going to be: is there a convenient way in jQuery to select every tabable object? All s s s and ???
In the accepted answer to this question Multiple submit buttons in an HTML form the ment is raised:
Please don't do this without also changing the tab order, so that hitting the tab button will cycle through the buttons as they appear on screen.
My question is: is there a way to set the tabindex on those two buttons to acplish this ordering without having to assign a specific tabindex to every other tabable element on the page?
My understanding of tabindex values is that specified positive values proceed elements w/o a specified value, so I am at a loss to figure out a way to do this w/o going through and assigning everything else a value.
If indeed, assigning a specific tabindex to every item is the only way, is there some (hopefully short and hopefully jQuery) magic to assign every appropriate element on the page a tabindex of, say, 1?
EDIT
As it looks like the solution is going to involve applying a specific tabindex to every other tabable object -- it seems like an important part of the solution is going to be: is there a convenient way in jQuery to select every tabable object? All s s s and ???
- did you review Adding tabindex dynamically? – Artem Commented Jan 22, 2016 at 23:35
2 Answers
Reset to default 3According to the specification:
positiv values assigned to
tabindex
orders the elements according to theirtabindex
valuesnegative values make elements "unfocusable"
a value of 0 makes the element focusable but its order dependents on the platform
mdn-html specification of tabindex
So if you want to have a specific order in your page you have to assign a value to each element.
But here es jquery:
Say the elements which should be in order are in a div
with id="myDiv"
You can then do:
$("#myDiv").find("*").prop("tabindex", 1);
This would make every child/subchild element of myDiv
have a tabindex
of 1.
Then your two buttons could have a css
class assigned (e.g: class="highTabIndex"
).
Then you can call jquery again:
var idx = 2;
$("#myDiv").find(".highTabIndex").each(function(idx, element) {
element.prop("tabindex", idx++);
});
and your buttons with class highTabIndex
would be orderd according to "position" in the page.
Using Adding tabindex dynamically and fixing button indecies:
$(":input:not(:hidden)")
.each(function (i) {
$(this).attr('tabindex', i + 1);
});
var r = $('input.r').attr('tabindex');
$('input.r').attr('tabindex', $('input.l').attr('tabindex'));
$('input.l').attr('tabindex', r);
html:
<input type="submit" value="Next" class="r" />
<input type="submit" value="Previous" class="l" />
Plunk
Update - fixed query to select not only inputs (check link in John's ment below):
$("a[href],area[href],input:not([disabled]),select:not([disabled]),\
textarea:not([disabled]),button:not([disabled]),iframe,[tabindex],\
[contentEditable=true]")
版权声明:本文标题:javascript - Setting tabindex to "last element" and "penultimate element" - possible? - Stac 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742289581a2447546.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论