Example of how to sort the hotspot of a JavaScript menu item

Source: Internet
Author: User

An example of how to sort the hotspot of a JavaScript menu item. list items or menu items are sorted by the number of clicks (or other events). Here is just an example, if you want to see a more complete example, you can develop it on this basis.

  DEMO code:

<Div> <ul id = "menu"> <li> <span> First </span> </li> <span> Second </span> </li> <li> <span> Third </span> </li> </ul> <p id = "status"> </p> <script> // This sorting method is a default mode // There are two default initial conditions: // first: the level value is not set. The default value is 0. // Second: The level value has been given, and the list // has been scaled from large to small (from top to bottom) arranged. // Therefore, you can compare it with the above elements. Var sortIt = function _ (elt) {var pes = elt. previuselementsibling; if (pes & elt. level> pes. level) {var ihtml = elt. innerHTML, ilevel = elt. level; elt. innerHTML = pes. innerHTML; elt. level = pes. level; pes. innerHTML = ihtml; pes. level = ilevel; _ (pes) ;}; // RBI: Hitting // here, the level value is the number of clicks (onclick event, // rank is the sorting number (starting from 0) by default ). // The more clicks (the larger the level value), the higher the rank. Function rbiIt (elts) {var I; for (I = 0; I <elts. length; I + = 1) {elts [I]. onclick = function (I) {var level = elts [I]. level | 0, rank = elts [I]. rank | I; return function (e) {this. level = ++ level; this. rank = rank; sortIt (this); document. getElementById ("status "). innerHTML = "(" + this. rank + ":" + this. level + ")" ;};}( I) ;}; // initialization // you can specify the level and rank value var elts = document. getElementById ("menu "). children; // var elts = document. getElementsByTagName ("li"); var k; for (k = 0; k <elts. length; k + = 1) {elts [k]. level = 0; // elts [k]. rank = 0;} rbiIt (elts); // you are welcome to copy bkjia.com, reject malicious collection of liehuo.net </script> </div> <br/> <center> If the page cannot be displayed, press Ctrl + F5 to refresh the page. For more webpage code: <a href =' http://www.bkjia.com/ 'Target = '_ blank'> http://www.bkjia.com/ </A> </center>

Tip: the code can be modified before running!

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.