JS右鍵點選事件
<html>
<body oncontextmenu = showMenu('')>
<form name = "menuForm">
<!--隱藏框,用來儲存選擇的選單的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>
<!-- 這裡用來定義需要顯示的右鍵選單 -->
<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)
{
//建立彈出選單
var pop=window.createPopup();
//設定彈出選單的內容
pop.document.body.innerHTML=menuDiv.innerHTML;
var rowObjs=pop.document.body.all[0].rows;
//獲得彈出選單的行數
var rowCount=rowObjs.length;
//迴圈設定每行的屬性
for(var i=0;i<rowObjs.length;i++)
{
//如果設定該行不顯示,則行數減一
var hide=rowControlString.charAt(i)!='1';
if(hide){
rowCount--;
}
//設定是否顯示該行
rowObjs[i].style.display=(hide)?"none":"";
//設定滑鼠滑入該行時的$#¥,·#t\u0007U\u0010H\u000Ea\u001Ei效果
rowObjs[i].cells[0].onmouseover=function()
{
this.style.background="#818181";
this.style.color="white";
}
//設定滑鼠滑出該行時的$#¥,·#t\u0007U\u0010H\u000Ea\u001Ei效果
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>