文章來(lái)源:http://www.souzz.net/html/web/htmlcss/9665.html
HTML中實(shí)現右鍵菜單功能
我們使用的應用系統很多都有右鍵菜單功能。但是在網(wǎng)頁(yè)上面,點(diǎn)擊右鍵一般顯示的卻是IE默認的右鍵菜單,那么我們如何實(shí)現自己的右鍵菜單呢?下面將講解右鍵菜單功能的實(shí)現原理和實(shí)現代碼。
實(shí)現原理
在HTML語(yǔ)言中,基本上每個(gè)對象都有一個(gè)oncontextmenu事件,這個(gè)事件就是鼠標的右鍵單擊事件(onclick事件是鼠標的左鍵單擊事件),那么我們就可以在鼠標右擊的時(shí)候,讓系統彈出一個(gè)窗口(這個(gè)是popup窗口,顯示在IE的最前面,沒(méi)有菜單),上面顯示我們想要顯示的菜單信息,當我們單擊其中某一項的時(shí)候,就執行我們設定的動(dòng)作,然后將彈出窗口關(guān)閉。
實(shí)現代碼
下面我寫(xiě)了一個(gè)示例代碼,模擬一個(gè)樹(shù)型菜單,當我們右鍵點(diǎn)擊樹(shù)型菜單某一項的時(shí)候,就會(huì )彈出右鍵菜單,里面有“新增”、“修改”、“刪除”三個(gè)菜單項,單擊某項會(huì )執行相應的操作。如果是在頁(yè)面的其它地方點(diǎn)擊右鍵的話(huà),就只顯示“新增”一個(gè)菜單項。下面的代碼內容:
contextmenuDemo.html文件
―――――――――――――――――――――――――――――――――
<%--
/**
*實(shí)現右鍵菜單功能
*/
--%>
<html>
<body oncontextmenu = showMenu('')>
<form name = "menuForm">
<!--隱藏框,用來(lái)保存選擇的菜單的id值-->
<input type = "hidden" name = "id" value = "">
<table>
<tr><td><a href="javascript:clickMenu()" oncontextmenu = showMenu('0')>根目錄</a></td></tr>
<tr><td><a href="javascript:clickMenu()" oncontextmenu = showMenu('1')>菜單一</a></td></tr>
<tr><td><a href="javascript:clickMenu()" oncontextmenu = showMenu('2')>菜單二</a></td></tr>
</table>
</form>
</body>
<!-- 這里用來(lái)定義需要顯示的右鍵菜單 -->
<div id="itemMenu" style="display:none">
<table border="1" width="100%" height="100%" bgcolor="#cccccc" style="border:thin" cellspacing="0">
<tr>
<td style="cursor:default;border:outset 1;" align="center" onclick="parent.create()">
新增
</td>
</tr>
<tr>
<td style="cursor:default;border:outset 1;" align="center" onclick="parent.update();">
修改
</td>
</tr>
<tr>
<td style="cursor:default;border:outset 1;" align="center" onclick="parent.del()">
刪除
</td>
</tr>
</table>
</div>
<!-- 右鍵菜單結束-->
</html>
<script language="JavaScript">
/**
*根據傳入的id顯示右鍵菜單
*/
function showMenu(id)
{
menuForm.id.value = id;
if("" == id)
{
popMenu(itemMenu,100,"100");
}
else
{
popMenu(itemMenu,100,"111");
}
event.returnValue=false;
event.cancelBubble=true;
return false;
}
/**
*顯示彈出菜單
*menuDiv:右鍵菜單的內容
*width:行顯示的寬度
*rowControlString:行控制字符串,0表示不顯示,1表示顯示,如“101”,則表示第1、3行顯示,第2行不顯示
*/
function popMenu(menuDiv,width,rowControlString)
{
//創(chuàng )建彈出菜單
var pop=window.createPopup();
//設置彈出菜單的內容
pop.document.body.innerHTML=menuDiv.innerHTML;
var rowObjs=pop.document.body.all[0].rows;
//獲得彈出菜單的行數
var rowCount=rowObjs.length;
//循環(huán)設置每行的屬性
for(var i=0;i<rowObjs.length;i++)
{
//如果設置該行不顯示,則行數減一
var hide=rowControlString.charAt(i)!='1';
if(hide){
rowCount--;
}
//設置是否顯示該行
rowObjs[i].style.display=(hide)?"none":"";
//設置鼠標滑入該行時(shí)的效果
rowObjs[i].cells[0].onmouseover=function()
{
this.style.background="#818181";
this.style.color="white";
}
//設置鼠標滑出該行時(shí)的效果
rowObjs[i].cells[0].onmouseout=function(){
this.style.background="#cccccc";
this.style.color="black";
}
}
//屏蔽菜單的菜單
pop.document.oncontextmenu=function()
{
return false;
}
//選擇右鍵菜單的一項后,菜單隱藏
pop.document.onclick=function()
{
pop.hide();
}
//顯示菜單
pop.show(event.clientX-1,event.clientY,width,rowCount*25,document.body);
return true;
}
function create()
{
alert("create" + menuForm.id.value + "!");
}
function update()
{
alert("update" + menuForm.id.value + "!");
}
function del()
{
alert("delete" + menuForm.id.value + "!");
}
function clickMenu()
{
alert("you click a menu!");
}
</script>
聯(lián)系客服