关闭

TreeView的自定义右键菜单(半原创)

标签: aspserverfunctionclassdiv浏览器
622人阅读 评论(1) 收藏 举报
分类:

http://www.cnblogs.com/liangjie/archive/2011/01/20/1940433.html

 

最近项目中有一个小小的需求,要求实现TreeView的自定义菜单,于是乎就收集资料,现将自己收集资料分享一下

1.示例

                                                                                              

                                      效果图

方法一 :利用oncontextmenu事件来实现

代码:

<html  >
<head runat="server">
    <title>xTreeView</title>
    <style type="text/css">
<!--
/*菜单样式*//*菜单项样式*/
.skin0{
position:absolute;
width:140px;
border:2px solid black;
background-color:menu;
font-family:Verdana;
line-height:20px;
cursor:default;
font-size:14px;
z-index:100;
visibility:hidden;
}
.menuitems
{
padding-left:10px;
padding-right:10px;
font-family:Verdana;
font-size:12px;
color:black;
}
-->
</style>

</head>
<body>
<form id="form1" runat="server" >
               <asp:Panel ID="Panel1" runat="server" Height="50px" Width="176px" >
               <asp:TreeView ID="TreeView1" runat="server" BackColor="#FFFFC0"  Height="180px" Width="172px">
                <Nodes>
                    <asp:TreeNode Text="Root" Value="sroot">
                        <asp:TreeNode  Text="Parent 1" Value="sParent 1">
                            <asp:TreeNode Text="<b id='b1' oncontextmenu='return showmenuie(event)'  >this is a test</b>" Value=

                                                        "sLeaf 1" ></asp:TreeNode>
                            <asp:TreeNode Text="Leaf 2" Value="sLeaf 2"></asp:TreeNode>
                        </asp:TreeNode>
                        <asp:TreeNode Text="Parent 2" Value="sParent 2">
                            <asp:TreeNode Text="Leaf 1" Value="sLeaf 1"></asp:TreeNode>
                            <asp:TreeNode Text="Leaf 2" Value="sLeaf 2"></asp:TreeNode>
                        </asp:TreeNode>
                    </asp:TreeNode>
                </Nodes>
            </asp:TreeView>
        </asp:Panel>
        <div>
        <asp:Panel ID="Panel2"  runat="server" display:none BorderColor="Black" CssClass="skin0" onMouseover="highlightie(event)" onMouseout="lowlightie(event)" onClick="jumptoie(event)"  >
                <div class="menuitems"><asp:LinkButton ID="LinkButton1" runat="server"   CssClass="menuitems" >New Node</asp:LinkButton></div>
                <div class="menuitems"><asp:LinkButton ID="LinkButton2" runat="server"   CssClass="menuitems">Edit Node</asp:LinkButton></div>
                <hr />
                <div class="menuitems"><asp:LinkButton ID="LinkButton3" runat="server"   CssClass="menuitems">Delete Node</asp:LinkButton></div>
                <hr />
                <div class="menuitems"><asp:LinkButton ID="LinkButton4" runat="server"   CssClass="menuitems">FAQS</asp:LinkButton></div>
                <div class="menuitems"><asp:LinkButton ID="LinkButton5" runat="server"   CssClass="menuitems">Online Help</asp:LinkButton></div>
                <hr />
                <div class="menuitems"><asp:LinkButton ID="LinkButton6" runat="server"   CssClass="menuitems">Email Me</asp:LinkButton></div>
            </asp:Panel>
         </div>
    </form> 
 <script src="xtreeview.js"  type="text/javascript"></script>
</body>
</html>
相应的Js代码:

   // xtreeView JScript File

var ie=document.all&&document.getElementById
var ns=document.getElementById&&!document.all
if (ie||ns)
var menuobj=document.getElementById("Panel2")

//屏蔽浏览器右键菜单并显示自定义右键菜单
function showmenuie(e) {

//设置右键菜单的位置
var rightedge=ie? document.body.clientWidth-event.clientX : window.innerWidth-e.clientX
var bottomedge=ie? document.body.clientHeight-event.clientY : window.innerHeight-e.clientY
if (rightedge<menuobj.offsetWidth)
       menuobj.style.left=ie? document.body.scrollLeft+event.clientX-menuobj.offsetWidth : window.pageXOffset+e.clientX-menuobj.offsetWidth
else
       menuobj.style.left=ie? document.body.scrollLeft+event.clientX : window.pageXOffset+e.clientX
if (bottomedge<menuobj.offsetHeight)
       menuobj.style.top=ie? document.body.scrollTop+event.clientY-menuobj.offsetHeight : window.pageYOffset+e.clientY-menuobj.offsetHeight
else
       menuobj.style.top=ie? document.body.scrollTop+event.clientY : window.pageYOffset+e.clientY

 

//让菜单显示
menuobj.style.visibility="visible"

//屏蔽浏览器右键
return false
}

function hidemenuie(e) {
//让菜单隐藏
      menuobj.style.visibility="hidden"
}

//当鼠标旋停在菜单的某项时高亮显示
function highlightie(e){
     var firingobj=ie? event.srcElement : e.target
     if (firingobj.className=="menuitems"||ns&&firingobj.parentNode.className=="menuitems"){
                if (ns&&firingobj.parentNode.className=="menuitems")

                                {
                                   firingobj=firingobj.parentNode

                                 }

                firingobj.style.backgroundColor="highlight"

}
}

//当鼠标离开在菜单的某项时某项正常显示
function lowlightie(e){
    var firingobj=ie? event.srcElement : e.target
    if (firingobj.className=="menuitems"||ns&&firingobj.parentNode.className=="menuitems"){
                 if (ns&&firingobj.parentNode.className=="menuitems")
                        {

                           firingobj=firingobj.parentNode

                        }

                firingobj.style.backgroundColor=""
                window.status=''
  }
}

function jumptoie(e){
    var firingobj=ie? event.srcElement : e.target
    if (firingobj.className=="menuitems"||ns&&firingobj.parentNode.className=="menuitems"){
             if (ns&&firingobj.parentNode.className=="menuitems")
                    var firingobj2=firingobj.parentNode

    }
}

if (ie||ns){
     menuobj.style.display=''
     document.onclick=hidemenuie

}

注意:

    1.红色部分不能缺少return(少了return哪怕你在body里加上oncontextmenu=“return false”也不行):只要有oncontextmenu事件就会有浏览器右键菜单,除非你

           事件返回值为false

           

方法二利用onmouseup或者onmousedown来实现

只要在以上代码中去掉oncontextmenu='return showmenuie(event)'加上onmousedup='test(event)'或者onmousedown='test(event)'并加上

oncontextmenu="return false"即可。其中test(加到原JS代码中)定义如下:

function test(event) {
    if (event.button == 2) {
        showmenuie(event);
    }
}

至于其中的细节,可以去查看其他资料。两种方法原理一样。           

原示例来源:http://www.cnblogs.com/blodfox777/archive/2008/08/29/1279675.html

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:121111次
    • 积分:1482
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:163篇
    • 译文:0篇
    • 评论:15条
    文章存档
    最新评论