`
chengyue2007
  • 浏览: 1468891 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

javascript右键菜单(所有浏览器)

阅读更多

<HTML>  
<HEAD>  
<TITLE> New Document </TITLE>  
<SCRIPT LANGUAGE="JavaScript">   
<!--  
window.Menu=function(isDir,text,handle)  
{  
    this.IsDirectory=false;    //是否是目录      
    this.HTMLObj=null;    //关联HTML对象  
    this.ParentMenu=null;    //父菜单  
    this.SubMenus=[];    //存储子菜单数组  
    this.Text="";  
    this.ZIndex=900;    //层  
    this.Handle=null;    //单击时所执行的语句,目录不支持此属性  
    if(typeof(isDir)!="undefined" && isDir)  
    {  
        this.IsDirectory=true;  
    }  
    if(typeof(text)!="undefined")  
    {  
        this.Text=text;  
    }  
    if(typeof(handle)!="undefined")  
    {  
        this.Handle=handle;  
    }  
    //创建并追加子菜单  
    this.CreateSubMenu=function(isDir,text,handle)  
    {  
        if(this.IsDirectory)  
        {  
            var oMenu=new Menu();  
            if(typeof(isDir)!="undefined")  
            {  
                oMenu.IsDirectory=isDir;  
            }  
            if(typeof(text)!="undefined")  
            {  
                oMenu.Text=text;  
            }  
            if(typeof(handle)!="undefined")  
            {  
                oMenu.Handle=handle;  
            }  
            this.AppendSubMenu(oMenu);  
            return oMenu;  
        }  
        alert("出现错误,该对象不支持CreateSubMenu方法");  
        return null;  
    }  
    //追加子菜单  
    this.AppendSubMenu=function(oMenu)  
    {  
        this.SubMenus.push(oMenu);  
        oMenu.ParentMenu=this;  
        oMenu.ZIndex=this.ZIndex+1;  
    }  
    //插入子菜单  
    this.InsertSubMenu=function(oMenu,iAlign)  
    {  
        if(iAlign>=this.SubMenus.length)  
        {  
            this.SubMenus.push(oMenu);  
        }  
        else 
        {  
            this.SubMenus.splice(iAlign,0,oMenu);  
        }  
        oMenu.ParentMenu=this;  
        oMenu.ZIndex=this.ZIndex+1;  
    }  
    //移除子菜单  
    this.RemoveSubMenu=function(iAlign)  
    {  
        var RemoveArr=this.SubMenus.splice(iAlign,1);  
        if(RemoveArr.length>0)  
        {  
            RemoveArr[0].HTMLObj.parentNode.removeChild(RemoveArr[0].HTMLObj);  
        }  
    }  
    //把子菜单的数据转换成HTML格式  
    this.Create=function()  
    {  
        if(!this.IsDirectory)  
        {  
            alert("出现错误,该对象不支持Create方法");  
            return false;  
        }  
        var ParentElement=document.createElement("div");  
        this.ChildMenuHTMLObj=ParentElement;    //关联子菜单的HTML对象容器  
        ParentElement.style.cursor="default";  
        ParentElement.onmousedown=function()  
        {  
            window.event.cancelBubble=true;  
        }  
        ParentElement.onselectstart=function()  
        {  
            return false;  
        }  
        ParentElement.style.position="absolute";  
        ParentElement.style.width="0px";  
        ParentElement.style.visibility="hidden";  
        ParentElement.style.zIndex=this.ZIndex;  
        ParentElement.style.border="1px solid #464646";      
        var table=document.createElement("table");  
        table.border=0;  
        table.cellPadding=0;  
        table.cellSpacing=0;  
        var tbody=document.createElement("tbody");  
        table.appendChild(tbody);  
        var tr=document.createElement("tr");  
        var ltd=document.createElement("td");  
        var rtd=document.createElement("td");  
        tr.appendChild(ltd);  
        tr.appendChild(rtd);  
        tbody.appendChild(tr);  
        ltd.style.width="25px";  
        ltd.style.backgroundImage="url(http://www.fjcjhr.com/bg.gif)";  
        ParentElement.appendChild(table);  
        var len=this.SubMenus.length;  
        if(len>0)  
        {  
            var ChildTable=document.createElement("table");  
            var ChildTBody=document.createElement("tbody");  
            ChildTable.appendChild(ChildTBody);  
            ChildTable.border=0;  
            ChildTable.cellPadding=0;  
            ChildTable.cellSpacing=0;  
            ChildTable.style.fontSize=Menu.Config.FontSize;  
            ChildTable.style.color=Menu.Config.FontColor;  
            rtd.appendChild(ChildTable);  
        }  
        for(var i=0;i<len;i++)  
        {  
            var tempTr=document.createElement("tr");  
            //关联HTML对象和DATA对象  
            this.SubMenus[i].HTMLObj=tempTr;    //关联子菜单的HTML对象  
            tempTr.DataObj=this.SubMenus[i];  
            var tempTd=document.createElement("td");  
            tempTr.style.backgroundColor=Menu.Config.BgColor;  
            tempTr.appendChild(tempTd);  
            tempTd.style.height=Menu.Config.PerMenuHeight;  
            tempTd.vAlign="middle";              
            tempTd.style.wordWarp="normal";  
            tempTd.style.paddingLeft="5px";  
            tempTd.style.paddingRight="5px";  
            tempTr.onmouseover=this.SubMenus[i].MouseOver;  
            tempTr.onmouseout=this.SubMenus[i].MouseOut;  
            tempTr.onclick=this.SubMenus[i].Click;  
            tempTd.appendChild(document.createTextNode(this.SubMenus[i].Text));  
            var DirectoryTd=document.createElement("td");  
            if(this.SubMenus[i].IsDirectory)  
            {  
                var font=document.createElement("font");  
                font.style.fontFamily="webdings";  
                font.appendChild(document.createTextNode("4"));  
                DirectoryTd.appendChild(font);  
            }  
            tempTr.appendChild(DirectoryTd);  
            ChildTBody.appendChild(tempTr);  
        }  
        document.body.appendChild(ParentElement);  
        for(var i=0;i<len;i++)  
        {  
            if(this.SubMenus[i].IsDirectory)  
            {  
                this.SubMenus[i].Create();  
            }  
        }  
    }  
    this.Show=function(e)  
    {  
        if(!this.IsDirectory)  
        {  
            alert("出现错误,该对象不支持Show方法");  
            return false;  
        }  
        if(this.SubMenus.length==0)    return;  
        var ChildHTMLObj=this.ChildMenuHTMLObj;  
        var DWidth=document.body.clientWidth;  
        var DHeight=document.body.clientHeight;  
        var left=document.body.scrollLeft,top=document.body.scrollTop;  
        var x,y;  
        if(this.ParentMenu==null)    //根对象  
        {  
            x=e.clientX,y=e.clientY;  
            if(x+ChildHTMLObj.offsetWidth>DWidth)  
            {  
                x-=ChildHTMLObj.offsetWidth;  
            }  
            if(y+ChildHTMLObj.offsetHeight>DHeight)  
            {  
                y-=ChildHTMLObj.offsetHeight;  
            }  
            x+=left;  
            y+=top;  
        }  
        else 
        {  
            var CurrentHTMLObj=this.HTMLObj;  
            var x=Menu.GetMenuPositionX(CurrentHTMLObj)+CurrentHTMLObj.offsetWidth,y=Menu.GetMenuPositionY(CurrentHTMLObj);  
            if(x+ChildHTMLObj.offsetWidth>DWidth+left)  
            {  
                x-=(CurrentHTMLObj.offsetWidth+ChildHTMLObj.offsetWidth);  
            }  
            if(y+ChildHTMLObj.offsetHeight>DHeight+top)  
            {  
                y-=ChildHTMLObj.offsetHeight;  
                y+=CurrentHTMLObj.offsetHeight;  
            }  
        }  
        ChildHTMLObj.style.left=x;  
        ChildHTMLObj.style.top=y;  
        this.ChildMenuHTMLObj.style.visibility="visible";  
    }  
    this.Hidden=function()  
    {          
        if(!this.IsDirectory)  
        {  
            alert("出现错误,该对象不支持Hidden方法");  
            return false;  
        }  
        var len=this.SubMenus.length;  
        for(var i=0;i<len;i++)  
        {  
            if(this.SubMenus[i].IsDirectory)  
            {  
                this.SubMenus[i].Hidden();  
            }  
        }  
        this.ChildMenuHTMLObj.style.visibility="hidden";  
    }  
    this.MouseOver=function()  
    {  
        this.style.backgroundColor=Menu.Config.OverBgColor;  
        var ParentMenu=this.DataObj.ParentMenu;  
        var len=ParentMenu.SubMenus.length;  
        for(var i=0;i<len;i++)  
        {  
            if(ParentMenu.SubMenus[i].IsDirectory)  
            {  
                ParentMenu.SubMenus[i].Hidden();  
            }  
        }  
        if(this.DataObj.IsDirectory)  
        {  
            this.DataObj.Show();  
        }  
    }  
    this.MouseOut=function()  
    {  
        this.style.backgroundColor=Menu.Config.BgColor;  
    }  
    this.Clear=function()  
    {  
        if(this.IsDirectory)  
        {  
            var len=this.SubMenus.length;  
            for(var i=0;i<len;i++)  
            {  
                if(this.SubMenus[i].IsDirectory)  
                {  
                    this.SubMenus[i].Clear();  
                }  
            }  
        }  
        document.body.removeChild(this.ChildMenuHTMLObj);  
    }  
    this.Click=function()  
    {      
        if(!this.DataObj.IsDirectory)  
        {  
            eval(this.DataObj.Handle);  
            Menu.Config.FirstMenu.Hidden();  
        }  
    }  
}  
//菜单配置  
Menu.Config=  
{  
    FirstMenu:new Menu(true),    //系统定义的第一个菜单,必须为容器(IsDirectory=true)  
    BgColor:"#FFFFFF",    //设置菜单背景颜色  
    OverBgColor:"#B5BED6",    //设置菜单鼠标经过时的背景颜色  
    FontSize:"13px",    //设置菜单字体大小  
    FontColor:"#000000",    //设置菜单字体颜色  
    PerMenuHeight:"25px"    //调整菜单的行距  
};  
Menu.GetMenuPositionX=function(obj)  
{  
    var ParentObj=obj;  
    var left;  
    left=ParentObj.offsetLeft;  
    while(ParentObj=ParentObj.offsetParent){  
        left+=ParentObj.offsetLeft;  
    }  
    return left;  
}  
Menu.GetMenuPositionY=function(obj)  
{  
    var ParentObj=obj;  
    var top;  
    top=ParentObj.offsetTop;  
    while(ParentObj=ParentObj.offsetParent){  
        top+=ParentObj.offsetTop;  
    }  
    return top;  
}  
Menu.Update=function()  
{  
    var FirstMenu=Menu.Config.FirstMenu;  
    FirstMenu.Clear();  
    FirstMenu.Create();  
}  
//事件  
window.onload=function()  
{      
    Menu.Config.FirstMenu.Create();  
    document.oncontextmenu=function(e)  
    {  
        e=e||event  
        Menu.Config.FirstMenu.Show(e);  
        return false;  
    }  
    document.onmousedown=function()  
    {  
        Menu.Config.FirstMenu.Hidden();  
    }  
}  
//*************************************系统实例******************************************  
window.CXP_Menu=Menu.Config.FirstMenu;  
var pg=CXP_Menu.CreateSubMenu();  
pg.Text="程序(P)";  
pg.IsDirectory=true;  
var wd=new Menu(true,"文档(D)");  
CXP_Menu.AppendSubMenu(wd);  
var set=CXP_Menu.CreateSubMenu(true,"设置(S)");  
 
var help=CXP_Menu.CreateSubMenu(false,"帮助(H)");  
help.Handle="alert('这是帮助!')";  
var run=CXP_Menu.CreateSubMenu(false,"运行(R) ...","alert('这是运行!')");  
var exit=new Menu();  
exit.Text="关机(U) ...";  
CXP_Menu.AppendSubMenu(exit);  
//插入菜单  
CXP_Menu.InsertSubMenu(new Menu(false,"Windows Update","if(confirm('此处通向MS官方,您真的要去吗?'))location.href='http://windowsupdate.microsoft.com/'"));  
pg.AppendSubMenu(new Menu(false,"程序1 ..."));  
pg.AppendSubMenu(new Menu(false,"程序2"));  
pg.AppendSubMenu(new Menu(false,"程序4"));  
pg.AppendSubMenu(new Menu(false,"程序5"));  
pg.AppendSubMenu(new Menu(false,"程序6"));  
wd.AppendSubMenu(new Menu(false,"文档1 ..."));  
wd.AppendSubMenu(new Menu(false,"文档2"));  
set.AppendSubMenu(new Menu(false,"设置1 ..."));  
set.AppendSubMenu(new Menu(false,"设置2"));  
var pg3=new Menu(true,"程序3");  
pg3.AppendSubMenu(new Menu(false,"程序4"));  
pg3.AppendSubMenu(new Menu(false,"程序5"));  
pg3.AppendSubMenu(new Menu(false,"程序6 ......."));  
pg.AppendSubMenu(pg3);  
function change()  
{  
    exit.Text="关鸡!";  
    Menu.Update();  
}  
function add()  
{  
    CXP_Menu.AppendSubMenu(new Menu(false,"我是新加的!"));  
    Menu.Update();  
}  
function del()  
{  
    CXP_Menu.RemoveSubMenu(0);  
}  
//-->  
</SCRIPT>  
</HEAD>  
<BODY bgcolor=#000000 leftmargin=0 topmargin=0 style="color:#FFFFFF" mce_style="color:#FFFFFF">  
<input type="button" value="添加新菜单" onclick="add()">  
<input type="button" value="把关机修改成关鸡^_^" onclick="change()">  
<input type="button" value="删除顶菜单" onclick="del()">  
good  
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>  
</BODY>  
</HTML>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics