JavaScript CSS Menu features improved version _ navigation menu

Source: Internet
Author: User
The improved version optimizes the problem by adding a menu to a simple JavaScript code. At the same time make the HTML page is very concise, only need to write 2 lines of code can! O (∩_∩) o
1. Use the premises, introduce a CSS file in the HTML page, and a JavaScript file. As follows:
Copy Code code 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 CSS file: <link type= "Text/css" rel= "stylesheet" href= "Menu.css" >, MENU.CSS code See after
Introducing JavaScript Files: <script src= "Menu.js" ></script>
2. Define menu code as follows:
Copy Code code 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 as"));
M1.add (New MenuItem ("close"));
M1.add (New MenuItem (""));

var m2 = new Menu ("Edit");
Root.add (m2);

Root.tostring ();
}

Description
1) var root = new Root ();
Root.tostring ();
Fixed format
2) Declaration Menu:
var m1 = new Menu ("File", "alert (This.innertext);");
The menu displays the name "File", and the OnClick event is alert (This.innertext);
Root.add (M1);
The first Level menu (the menu that the page initially displays) is placed under root, using the Add () method
var M11 = new MenuItem ("new");
M1.add (M11);
Declares the submenu "New" for "File"
M1.add (New MenuItem ("Open", "alert (' Open file ');");
Declares "File" submenu "Open"
The above code can complete the menu add function.
Code files:
<1> Menu.css
Copy Code code 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 a {
Border-style:none;
Color:black;
Display:block;
width:150px;
height:20px;
line-height:20px;
padding-left:10px;
Text-decoration:none;
}
/* the ' the ' the ' the ' the ' the ' displays ' Default/
#menubar. menumain{
Border-color: #C0C0C0;
border-width:1px;
Border-style:solid;
}
/* the ' 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 Li ul
Display:none;
Position:absolute;
top:0px;
left:150px;
margin-top:0;
margin-left:0;
width:150px;
}

<2>menu.js
Copy Code code 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 (' BeforeEnd ', 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:menuabstractnode
* Description:menuabstractnode Class
* @param {String} ptext
* @param {String} paction
* @Return:
*******************************************/
function Menuabstractnode (Ptext, paction) {
This.text = Ptext | | Menuconfig.defaulttext;
this.action = Paction | | Menuconfig.defaultaction;
This.id = Menuhandler.getid ();

This.childnodes = [];
}

MenuAbstractNode.prototype.add = function (node) {
This.childnodes[this.childnodes.length] = node;
}

/******************************************
* Funciont name:tostring
* Description:generate HTML Code
* @param
* @param
* @Return:
*******************************************/
MenuAbstractNode.prototype.toString = function () {
var str = "<li id=\" "+ this.id +" \ "onmouseover=\" Displaysubmenu (this) \ "Onmouseout=\" Hidesubmenu (this) \ ><a h Ref=\ "#\";

if (this.type== "menu") {
str = str + "class=\" "+ This.cssstyle +" "";
}
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 = Menuabstractnode;
This.base (ptext,paction);

This.type = "Menu";
This.cssstyle = Pcssstyle | | Menuconfig.defaultmenucssstyle;
}

Menu.prototype = new Menuabstractnode;

/******************************************
* Funciont Name:menuitem
* Description:menuitem Class
* @param {String} ptext
* @param {String} paction
* @Return:
*******************************************/
function MenuItem (Ptext, paction) {
This.base = Menuabstractnode;
This.base (ptext,paction);
This.type = "MenuItem";
}

Menuitem.prototype = new Menuabstractnode;


/******************************************
* Funciont Name:root
* Description:root Class
* @Return:
*******************************************/
function Root () {
This.id = "menubar";
This.childnodes=[];
}

Root.prototype = new Menuabstractnode;

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 as"));
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.