JavaScript TabIndex Properties _javascript tips

Source: Internet
Author: User
The use of TabIndex is very simple, is the use of the tab to traverse the page's form elements and links, according to the size of the TabIndex determines the order. Although insignificant, but the details of the real kungfu, this is any Web application should have the pro force, to ensure that users without the mouse, such as WAP, still can be used normally.

The following example, in order to highlight the TabIndex control focus on the ability to jump, deliberately upset the order. Select the first text field first, and then press the TAB key to observe.
<!doctype html> <ptml dir= "ltr" lang= "ZH-CN" > <pead> <meta charset= "gb2312"/> <meta http-eq uiv= "x-ua-compatible" content= "ie=7" > <title>tabindex by Masaki </title> <script> window.onload = Fu Nction () {var els = document.getelementsbytagname ("input"); for (Var i=0,n=els.length;i<n;i++) {Els[i].onfocus = function () {this.style.csstext= ' background: #69C; border-color : #6cc; '; }; Els[i].onblur = function () {this.style.csstext= ' background: #fff; Border-color: #000; '; }; } </script> </pead> <body> <form name= "Nasami" > <input tabindex= "1" value= "first" type= "t Ext "> <input tabindex=" 3 "value=" third "type=" text "> <input tabindex=" 5 "value=" fifth "type=" text "> &L T;input tabindex= "6" value= "sixth" type= "text" > <input tabindex= "4" value= "fourth" type= "text" > <input tabind ex= "2" value= "second" type= "text" > </form> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

According to the introduction of this article , the DOM and Netscape only add TabIndex to a limited number of elements: A, area, button, input, object, select, TextArea, What is called a form element and link. IE4 is more than the following elements: applets, body, Div, embed, isindex, marquee, span, table, and TD, to the IE5, almost all elements that can be rendered have this attribute (such as a BR element is not rendered). The value of TabIndex, according to the rules of the World wide area, ranges from 0 to 32767.

In the jquery source, the attr section provides a link to how to use JavaScript settings to get and remove TabIndex properties, but for some time, many major browsers have updated their versions. So I tested it again. The test program is shown in the following run box:

<textarea id="runcode55492"><?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" > < HTML xmlns= "http://www.w3.org/1999/xhtml" > <pead> <meta http-equiv= "Content-type" content=; charset=gb2312 "/> <title>tabindex by Masaki </title> <script type=" Text/javascript "> <!--//-- ><! [cdata[//><!--window.onload = function () {var el = document.getElementById ("test"); Alert (el.tabindex) alert (El.getattribute ("TabIndex")) Alert (El.getattribute ("TabIndex"))}//--><!] > </script> </pead> <body> <input tabindex= "1" value= "test" type= "text" id= "Test" > </b Ody> </ptml></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

INPUT element with TabIndex 1 IE6 IE7 IE8 FF3.55 opeta10.10 Safari4.0 chrome4.02
El.tabindex 1 1 1 1 1 1 1
El.getattribute ("TabIndex") 1 1 1 1 1 1 1
El.getattribute ("TabIndex") 1 1 1 1 1 1 1

This table is 1 compared to the corresponding table in the Getting, setting, and removing TabIndex values with JavaScript.

Let's see if the TabIndex property is set when the form element is not displayed, and the default value is TabIndex if it exists.

INPUT element with no TabIndex IE6 IE7 IE8 FF3.55 opeta10.10 Safari4.0 chrome4.02
El.tabindex 0 0 0 0 0 0 0
El.getattribute ("TabIndex") 0 0 0 Null Null Null Null
El.getattribute ("TabIndex") 0 0 0 Null Null Null Null

What if the test object is a DIV element that does not explicitly set the TabIndex property? In the standard of the consortium, only form elements and links can tabindex attributes.

div element with no tabindex IE6 IE7 IE8 FF3.55 opeta10.10 Safari4.0 chrome4.02
El.tabindex 0 0 0 -1 -1 -1 -1
El.getattribute ("TabIndex") 0 0 0 Null Null Null Null
El.getattribute ("TabIndex") 0 0 0 Null Null Null Null

We read this article, the standard browser set it apart, -1 to those who should not have it, 0 is the default assigned to those form elements and links, if the user defined to return the user's value, even if it is div.

However, in IE, non-form elements and links, regardless of whether TabIndex is defined to return 0, then how do we know if the element has been defined? "Getting, setting,......" gives a very good idea. Use GetAttributeNode to get the corresponding attribute node. In IE, if it is a default property or a defined property, an object is returned, and the standard browser returns the object only if we set the property on display, all others null. In IE, if the default property is not assigned a value, it has a special specified that is displayed as false, and true if the value is assigned. Standard browsers do not have this thing, and do not need this thing.

And then look at the situation where there is no TabIndex attribute element assignment El.tabindex=value Needless to say, it certainly will work, Dom 0 era of implementation. If an element is assigned with setattribute ("TabIndex", 3), and the note is uppercase, then either El.tabindex or El.getattribute ("TabIndex") or El.getattribute (" TabIndex ") can get 3. If it is setattribute ("TabIndex", 3) assigned value, IE is all 0, the standard browser is all 3, so use the former bar.

INPUT element with no TabIndex IE6 IE7 IE8 FF3.55 opeta10.10 Safari4.0 chrome4.02
El.tabindex=3 3 3 3 3 3 3 3
El.setattribute ("TabIndex", 3) 3 3 3 3 3 3 3
El.setattribute ("TabIndex", 3) 0 0 0 3 3 3 3

Finally, the property is removed and the results are removed by El.tabindex,el.getattribute ("TabIndex"), El.getattribute ("TabIndex") tests. No one is sure to remove the clean, from the current situation, only select El.removeattribute("TabIndex").

INPUT element with TabIndex 3 IE6 IE7 IE8 FF3.55 opeta10.10 Safari4.0 chrome4.02
El.removeattribute("TabIndex") 0,0,0 0,0,0 0,0,0 0,null,null 0,null,null 3,null,null 3,null,null
El.removeattribute("TabIndex") 3,3,3 3,3,3 3,3,3 0,null,null 0,null,null 3,null,null 3,null,null
Delete El.tabindex Errer Errer Errer 3,3,3 0,null,null 3,3,3 Undefined,null,null
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.