Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 网站建设 > ASP > 用popup实现右键多级菜单
【标  题】:用popup实现右键多级菜单
【关键字】:popup
【来  源】:http://blog.csdn.net/adayfly/archive/2006/09/06/1185953.aspx

用popup实现右键多级菜单

Your Ad Here

网上已经有很多实现右键菜单的文章,本菜单也是在参考网上的经验之后写成。如有好的意见和建议欢迎留言

本菜单的特点:

1、可动态添加菜单项和子菜单

2、可用css控制菜单表现

菜单样式如下:

 

代码如下:

popmenu.css

 .menuMain {
  font-size: 12p;
  background: #ffffff;
  

  BORDER-RIGHT: gold 1px solid;
  BORDER-TOP: gold 1px solid;
  BORDER-LEFT: gold 1px solid;


 }
 .menuNormal
 {
  valign:middle;
  text-align:center;
  background-color:#ffffff;
  font-size: 12px;
         color:#000000;  
  cursor:default;
  FILTER:0;

  BORDER-BOTTOM: gold 1px solid;
 }
 .menuHot
 {
  valign:middle;
  text-align:center;
  background-color:#ffffff;;
  font-size: 12px;
         color:#ffffff;  
  cursor:default;
  FILTER:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=gold, EndColorStr=#FFFFFF);

  BORDER-BOTTOM: gold 1px solid;
  }

popmenu.js


//p.mainMenu.document.styleSheets
//右键菜单
/*
 cssFile:菜单模式css文件
 nomalClass:菜单子项的普通模式
 hotClass:菜单子项获得焦点模式
 menuClass:菜单模式
 menuWidth:菜单宽度
*/
PopMenu=function(cssFile,nomalClass,hotClass,menuClass,menuWidth)
{
 //初始化菜单式样
 this.nomalClass=nomalClass;
 this.hotClass=hotClass;
 this.menuClass=menuClass;
 this.menuWidth=menuWidth;
 this.cssFile=cssFile;
 this.menuItem=null;
 this.parentMenu=null;
 this.childMenu=null;
 this.childIndex=null;
 this.top=null;
 this.left=null;
 
}
//创建菜单
PopMenu.prototype.createMenu=function()
{
 this.menuItem=new Array();
 if(this.parentMenu!=null)
  this.mainMenu=this.parentMenu.mainMenu.document.parentWindow.createPopup();
 else
  this.mainMenu=window.createPopup();
  
 
 this.mainMenu.document.createStyleSheet(this.cssFile);
 
 this.menuTable=createTable(this.mainMenu.document,this.menuClass);
    this.mainMenu.document.oncontextmenu=function()
    {

            return false;

    }
    var pop=this.mainMenu;
    var mt=this;
   this.mainMenu.document.onclick=function()

    {
   clearSelMenu(mt);
            pop.hide();

    }

}
//创建子菜单
PopMenu.prototype.createChildMenu=function(index,menuWidth)
{
 if(this.childMenu!=null)
  this.childMenu.clear();
 else
 {
  this.childMenu=new PopMenu(this.cssFile,this.nomalClass,this.hotClass,this.menuClass,this.menuWidth)
  this.childMenu.parentMenu=this;
  this.childMenu.createMenu();
 }
 this.childIndex=index;
 this.childMenu.menuWidth=menuWidth;
 return this.childMenu;
}
//创建菜单项

/*

texttext:菜单项内容

clickFunciton:点击执行方法

menuId:菜单项ID(暂时未用到,留做扩充;

*/
PopMenu.prototype.addMenuItem=function(text,clickFunciton,menuId)
{
 var tindex=this.menuItem.length
 var menu={index:tindex,menuId:menuId}
 this.menuItem[tindex]=menu;
 
 var t=this.mainMenu.document.createElement("tr");
 var td=this.mainMenu.document.createElement('td');
 td.className=this.nomalClass;
 td.innerText=text;
 
 var pop=this;
 td.id=tindex
 var hc=this.hotClass;

 td.onmouseover=function()
    {
         this.className=pop.hotClass;
          
     //显示子菜单
            if(pop.childMenu!=null) pop.childMenu.hide();
            if(pop.childMenu!=null&&this.id==pop.childIndex)
            {
    clearSelMenu(pop.childMenu);

    var rowCount=pop.childMenu.menuTable.rows.length
    pop.childMenu.mainMenu.show(pop.menuWidth,25*pop.childIndex,pop.childMenu.menuWidth,rowCount*25,pop.mainMenu.document.body);
            }

    } 
    var pop2=this;

    td.onmouseout=function()
    {
  this.className=pop2.nomalClass;

    }
 t.appendChild(td);
    if(clickFunciton!=null) t.onclick=clickFunciton;
    this.menuTable.appendChild(t);

}
//显示菜单
PopMenu.prototype.showMenu=function()
{
 var rowCount=this.menuTable.rows.length

    this.mainMenu.show(event.clientX-1,event.clientY,this.menuWidth,rowCount*25,document.body);


}
//菜单隐藏
PopMenu.prototype.hide=function()
{
 this.mainMenu.hide();
}

//创建容纳菜单项的表格
function createTable(p,className)
{
 //var t=p.createElement('<table border="1" width="100%" height="100%" bgcolor="#ffffff" style="border:thin;font-size: 12px" cellspacing="0"  bordercolor="#336633" >');
 var t=p.createElement('<table width="100%" height="100%" cellpadding="0" cellspacing="0">');

 t.className=className;
 t.width="100%";
 t.height="100%";
 var tb=p.createElement("TBody");
 if(t!=null&&tb!=null);
  t.appendChild(tb);
  
  
 if(p!=null)
  p.body.appendChild(t);
 return tb;

}
//菜单复原,清除选择
clearSelMenu=function(pop)
{

    //循环设置每行的属性
    var mt=pop.menuTable

    for(var i=0;i<mt.children.length;i++)

    {
  mt.children[i].children[0].className=pop.nomalClass;
 
 }
}
//清除菜单项
PopMenu.prototype.clear=function()
{
 this.menuItem.length=0;
 if(this.menuTable.children.length==0) return;
    for(var i=this.menuTable.children.length-1;i>=0;i--)

    {
  this.menuTable.removeChild(this.menuTable.children[i]);
 
 }

}

/////////////////////////////

test.htm:

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="StyleSheet" href="popmenu.css" type="text/css" >

 


<script type="text/javascript" src="js/popmenu.js"></script>

<script language=javascript>
var popMenu;
function itemClick1(text)
{
 alert('item:'+text);
}
function itemClick2(text,index)
{
 alert('item2:'+text+','+index);
}
function childmenuClick(text,index)
{
 alert('childMenu:'+text+','+index);
}

function initPopmenu()
{
 
 //参数:菜单显示模式的css文件,菜单项普通模式,菜单项获得焦点模式,菜单整体模式,菜单宽度
 popMenu=new PopMenu('popmenu.css','menuNormal','menuHot','menuMain',100);

 popMenu.createMenu();//创建菜单
 var a='test1';
 var a2='test2';
 var a3=5;
 
 popMenu.addMenuItem('test1',function(){itemClick1(a);},null);
 popMenu.addMenuItem('test2',function(){itemClick2(a2,a3);},null);
 popMenu.addMenuItem('test3',null,null);
 popMenu.addMenuItem('test4',null,null); 
  //创建第二级菜单
 var childMenu=popMenu.createChildMenu(2,100);//参数:子菜单父亲的index(用于定位),菜单宽度
 var a4='childmenuclick';
 var a5=10;
 childMenu.addMenuItem('test3--1',function(){childmenuClick(a4,a5)},null)
 childMenu.addMenuItem('test3--2',null,null)
 
 var ddMenu=childMenu.createChildMenu(1,100);
 ddMenu.addMenuItem('test3--2--1',null,null)
 ddMenu.addMenuItem('test3--2--2',null,null)

}

function showMenu()

{

 popMenu.showMenu();
}  
  
  //////////

 </script>


</head>


<body leftmargin="0" topmargin="0"   onLoad="initPopmenu()"  oncontextmenu="showMenu(); return false;"  >

</body>
 
</html>

 

 

 

 

 

 

 

 

 

 

基于 Spring 框架的留言本:【上一篇】
mysql的日期和时间函数:【下一篇】
【相关文章】
  • Post button for popup post to del.icio.us
  • 浅论popup
  • 巧用JPopupMenu(一)
  • CreatePopup()函数的使用---弹出广告
  • 从linux上给windows发送popup消息
  • 梅花雪脚本控件集:MzPopupLayer 可盖住flash的层基类
  • 让论坛也可以像outlook2003一样有新帖子/留言Popup提示
  • createPopup & event
  • TMsnPopup 5.0 (修正)
  • TMsnPopup v5.0 (提示窗)
  • 【随机文章】
  • 近期开发完成一套Skin库(SkinSharp)
  • 企业综合布线系统工程设计参考大全技术发展3
  • exe文件双击不能执行的毛病
  • 创新谈-张宪伟
  • 操作系统简史
  • 第一阶段 尝试建立CHORD P2P网络
  • 打造计数器DIY三步曲(下)
  • IT售前精要ICEBREAKER理论
  • Struts标签
  • solaris8学习资料 - 第二课
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.