CSS JavaScript (simplified version) for menu functions

Source: Internet
Author: User

The improved version optimizes this problem by using simple Javascript Code You can add a menu. At the same time, the HTML page is very concise. You only need to write two lines of code! O (partition _ partition) O
1. Prerequisite: Introduce a CSS file and a Javascript file to the HTML page. As follows: Copy code The Code is as follows: <! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> menu </title>
<Link type = "text/CSS" rel = "stylesheet" href = "menu.css">
</Head>
<Body>
<Div> <SCRIPT src = "menu. js"> </SCRIPT> </div>
</Body>
</Html>

Introduce the CSS file: <link type = "text/CSS" rel = "stylesheet" href = "menu.css">. For more information about the menu.css code, see
Introduce JavaScript files: <SCRIPT src = "menu. js"> </SCRIPT>
2. The menu definition code is as follows:Copy codeThe Code is as follows: if (document. getelementbyid ){
VaR root = New Root ();

VaR M1 = new menu ("file", "alert (this. innertext );");
Root. Add (M1 );
VaR M11 = new menuitem ("new ");
M1.add (m11 );
M1.add (New menuitem ("open", "alert ('Open file ');"));
VaR M12 = new menuitem ("save ");
M1.add (m12 );
M1.add (New menuitem ("Save "));
M1.add (New menuitem ("close "));
M1.add (New menuitem (""));

VaR m2 = new menu ("edit ");
Root. Add (m2 );

Root. tostring ();
}

Note:
1) var root = New Root ();
Root. tostring ();
Fixed format
2) Declaration menu:
VaR M1 = new menu ("file", "alert (this. innertext );");
The name displayed in the menu is "file", and The onclick event is alert (this. innertext );
Root. Add (M1 );
Place the first-level menu (that is, the menu initially displayed on the page) under the root, and use the add () method
VaR M11 = new menuitem ("new "");
M1.add (m11 );
Declare "file" sub-menu "new"
M1.add (New menuitem ("open", "alert ('Open file ');"));
Declare "file" sub-menu "open"
You can use the code above to add a menu.
Code file:
<1> menu.css Copy code The Code is as follows: # menubar {
Font-family: verdana;
Font-size: 12px;
Margin: 1px;
}
# Menubar Li {
Float: left;
Position: relative;
Text-align: left;
}
/* Each menu item style */
# Menubar Li {
Border-style: none;
Color: black;
Display: block;
Width: 150px;
Height: 20px;
Line-Height: 20px;
Padding-left: 10px;
Text-Decoration: none;
}
/* The first level menu which displays default */
# Menubar. menumain {
Border-color: # c0c0c0;
Border-width: 1px;
Border-style: solid;
}
/* The first leve style when mouse on it */
# Menubar Li A: hover {
Background-color: # efefef;
Text-Decoration: underline;
}
/* The second level menu block style */
# Menubar Li ul {
Background-color: # efefef;
Border-style: none;
Display: none;
Position: absolute;
Top: 20px;
Left:-40px;
Margin-top: 2px;
Width: 150px;
}
/* The sub menu item style when mouse on it */
# Menubar Li ul Li A: hover {
Text-Decoration: underline;
Padding-left: 20px;
}
/* The third or more level menu block style */
# Menubar Li ul {
Display: none;
Position: absolute;
Top: 0px;
Left: 150px;
Margin-top: 0;
Margin-left: 0;
Width: 150px;
}

<2> menu. jsCopy codeThe Code is as follows: var menuconfig = {
Defaulttext: "menu item ",
Defaultaction: "javascript: void (0 );",
Defaultmenucssstyle: "menumain"
};

VaR menuhandler = {
Idcounter: 0,
Idprefix: "menu -",
GETID: function () {return this. idprefix + this. idcounter ++ ;},
Inserthtmlbeforeend: function (node, shtml ){
If (node. insertadjacenthtml! = NULL ){
Node. insertadjacenthtml ('foreend', shtml );
Return;
}
VaR DF; // documentfragment
VaR r = node. ownerdocument. createRange ();
R. selectnodecontents (node );
R. Collapse (false );
DF = R. createcontextualfragment (shtml );
Node. appendchild (DF );
}
}

Function displaysubmenu (LI ){
VaR submenu = Li. getelementsbytagname ('ul ') [0];
If (submenu)
Submenu. style. Display = 'block ';
}

Function hidesubmenu (LI ){
VaR submenu = Li. getelementsbytagname ('ul ') [0];
If (submenu)
Submenu. style. Display = 'none ';
}

/*************************************** ***
* Funciont name: menuw.actnode
* Description: menustmactnode class
* @ Param {string} ptext
* @ Param {string} paction
* @ Return:
**************************************** ***/
Function menustmactnode (ptext, paction ){
This. Text = ptext | menuconfig. defaulttext;
This. Action = paction | menuconfig. defaultaction;
This. ID = menuhandler. GETID ();

This. childnodes = [];
}

Menustmactnode. Prototype. Add = function (node ){
This. childnodes [This. childnodes. Length] = node;
}

/*************************************** ***
* Funciont name: tostring
* Description: generate HTML code
* @ Param
* @ Param
* @ Return:
**************************************** ***/
Menustmactnode. Prototype. tostring = function (){
VaR STR = "<li id = \" "+ this. ID + "\" onmouseover = \ "displaysubmenu (this) \" onmouseout = \ "hidesubmenu (this) \"> <a href = \"#\"";

If (this. type = "menu "){
STR = STR + "class = \" "+ this.css style + "\"";
}
STR = STR + "onclick = \" "+ this. Action +" \ ">" + this. Text + "</a> ";

VaR sb = [];

For (VAR I = 0; I <this. childnodes. length; I ++ ){
SB [I] = This. childnodes [I]. tostring ();
}
If (sb. length> 0 ){
STR = STR + "<ul>" + sb. Join ("") + "</ul>"
}

Return STR + "</LI> ";
}

/*************************************** ***
* Funciont name: Menu
* Description: menu class
* @ Param {string} ptext
* @ Param {string} paction
* @ Param {string} pcssstyle
* @ Return:
**************************************** ***/
Function menu (ptext, paction, pcssstyle ){
This. base = menuw.actnode;
This. Base (ptext, paction );

This. type = "menu ";
This.css style = pcssstyle | menuconfig. defaultmenucssstyle;
}

Menu. Prototype = new menuw.actnode;

/*************************************** ***
* Funciont name: menuitem
* Description: menuitem class
* @ Param {string} ptext
* @ Param {string} paction
* @ Return:
**************************************** ***/
Function menuitem (ptext, paction ){
This. base = menuw.actnode;
This. Base (ptext, paction );
This. type = "menuitem ";
}

Menuitem. Prototype = new menuw.actnode;

/*************************************** ***
* Funciont name: Root
* Description: root class
* @ Return:
**************************************** ***/
Function root (){
This. ID = "menubar ";
This. childnodes = [];
}

Root. Prototype = new menuw.actnode;

Root. Prototype. tostring = function (){
Document. Write ("<Div id = 'menu '> <ul id = \" "+ root. ID +" \ "> </ul> </div> ");
For (VAR I = 0; I <this. childnodes. length; I ++ ){
Menuhandler. inserthtmlbeforeend (document. getelementbyid (root. ID), this. childnodes [I]. tostring ());
}
}

If (document. getelementbyid ){
VaR root = New Root ();

VaR M1 = new menu ("file", "alert (this. innertext );");
Root. Add (M1 );
VaR M11 = new menuitem ("new", "alert (this. innertext );");
M1.add (m11 );
M1.add (New menuitem ("open", "alert ('Open file ');"));
VaR M12 = new menuitem ("save ");
M1.add (m12 );
M1.add (New menuitem ("Save "));
M1.add (New menuitem ("close "));
M1.add (New menuitem (""));

VaR m2 = new menu ("edit ");
Root. Add (m2 );
VaR m22 = new menuitem ("select all ");
M2.add (m22 );
M2.add (New menuitem ("cut "));
M2.add (New menuitem ("copy "));
M2.add (New menuitem ("Paste "));

VaR m3 = new menu ("View ");
VaR M33 = new menuitem ("view List ");
M33.add (New menuitem ("function List "));
M3.add (M33 );
M3.add (New menuitem ("tool bar "));
Root. Add (m3 );
Root. tostring ();
}

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.