实现Web页面上的右键快捷菜单
样式代码:
body{font: 9pt "宋体"; margintop: 0px ; color: red; background: #ffffff}
a.{ font: 9pt "宋体"; cursor: hand; font-size: 9pt ; color: blue; text-decoration: none }
a:active{ font: 9pt "宋体"; cursor: hand; color: #FF0033 }
a.cc:hover{ font: 9pt "宋体"; cursor: hand; color: #FF0033}
.box{ font: 9pt "宋体"; position: absolute; background: LightGrey; }
html代码如下:
| 弹出菜单 |
| Erickson的专栏 |
| CSDN |
| Google 搜索 |
| 搜狐 |
| Yahoo |
| 163 网站 |
| 新浪网体育 |
脚本代码:
右击鼠标显示快捷菜单:
document.onmousedown = function popUp() {
menu = document.all.itemopen
if (event.button == 2) {
newX = window.event.x + document.body.scrollLeft
newY = window.event.y + document.body.scrollTop
menu.style.display = ""
menu.style.pixelLeft = newX
menu.style.pixelTop = newY
}
else if (event.button == 1)
{
menu.style.display = "none"
}
}
屏蔽IE默认的WinForm快捷菜单:
var message="";
function clickIE()
{
if (document.all)
{
(message);
return false;
}
}
function clickNS(e)
{
if (document.layers||(document.getElementById&&!document.all))
{
if (e.which==2)
{
newX = window.event.x + document.body.scrollLeft
newY = window.event.y + document.body.scrollTop
menu = document.all.itemopen
if ( menu.style.display == "")
{
menu.style.display = "none"
}
else
{
menu.style.display = ""
}
menu.style.pixelLeft = newX
menu.style.pixelTop = newY
}
if (e.which==3)
{
(message);
return false;
}
}
}
if (document.layers)
{
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS;
}
else
{
document.onmouseup=clickNS;document.oncontextmenu=clickIE;
}
document.oncontextmenu=new Function("return false")
菜单效果如下:


