Where do tabindex=“0” HTML elements end up in the tabbing order?

為{幸葍}努か 提交于 2019-12-03 08:08:46

问题


In what order are elements with a tabindex value of 0 focused when the web page is tabbed?


回答1:


The HTML specification states:

Elements that have identical tabindex values should be navigated in the order they appear in the character stream.




回答2:


tabindex assignments are handled the following way (for elements that support the tabindex attribute):

  • Positive numbers (1,2,3...32767) are handled in tab order.
  • 0 is handled in source order (the order it appears in the DOM)
  • -1 is ignored during tabbing but is focusable.

This information is taken from : http://www.w3.org/TR/html401/interact/forms.html#adef-tabindex




回答3:


It's a bit more complicated than Alan Haggai Alavi's answer.

After parsing, IE8 and Opera do as the HTML4 spec says. Firefox and Chrome however use DOM order. This matters with malformed markup like this.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>Test case 1</title>
  </head>
  <body>
    <form>
      <table>
        <tr><td><input id="first" value="first in the character stream" tabindex="0"></td></tr>
        <div><input id="second" value="second in the character stream" tabindex="0"></div>
      </table>
    <form>
  </body>
</html>

You might well argue that with malformed mark-up all bets are off anyway, so what about JavaScript?

Consider this case:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>Test case 2</title>
    <script type="text/javascript">
      moveFirst = function()
      {
        var f = document.getElementById("first");
        f.parentNode.removeChild(f);
        document.body.appendChild(f);
      }
    </script>
  </head>
  <body>
    <form>
      <table>
        <tr><td><input id="first" value="first in the character stream" tabindex="0"></td></tr>
        <tr><td><div><input id="second" value="second in the character stream" tabindex="0"></div></td></tr>
      </table>
    <form>
    <div onclick="moveFirst()">move</div>
  </body>
</html>

In this case, when a user clicks on "move", IE8, Firefox, Chrome and Opera all use DOM order, not character stream order.

Finally HTML5 offers pretty much no guarantees about the tab order between elements that have a tabindex of 0, merely stating that it should follow platform conventions.




回答4:


An example is the best way to remember.

Assume you are pressing Tab key on the keyboard

<button tabindex="0">one</button>
<button tabindex="-1">two</button>
<button tabindex="0">three</button>
<button tabindex="5">four</button>

First four will be focused during Tab press (because tabindex is highest)
then one (because tabindex is 0, next highest, but 1st displayed in the document)
then three (because tabindex is 0, same as above, but 2nd displayed in the document)
then nothing else (The two is never focused because tabindex is -1)

Why is tabindex="0" required? anyway tab stops at a button, right?
True, you can also have tab stop for a div, like:

<div tabindex="0">some content</div>

For example, this is good when you want a screen reader stop to attempt to read a text in a div.

Hope that helped.




回答5:


tabindex="0" can include tabbing to non-page elements of the web browser, such as the URL address bar.

Tested to be the case for Firefox 32.03.



来源:https://stackoverflow.com/questions/4115953/where-do-tabindex-0-html-elements-end-up-in-the-tabbing-order

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!