放一个javascript右键菜单的代码
注册很长时间号了,刚开通博客,拿出一个自己写的javascript右键菜单,兼容IE,firefox浏览器
这是css代码
body{
font-size:12px;
}
.contextMenu{
filter:alpha(opacity=80);
opacity:0.8;
border:1px #0033FF solid;
background-color:#0099CC;
cursor:pointer;
width:120px;
position:absolute;
top:0;
left:0;
font-size:12px;
}
.cmddiv{
padding:2px;
border-bottom:1px #00FF99 dotted;
}
还有javascript代码
window.onload=function(){
var contextMenu;
var buttons=['菜单1','菜单2','菜单3','菜单4'];
window.document.body.oncontextmenu=function(e){
var xy=getXY(e);
if(typeof contextMenu!="object"){
contextMenu=document.createElement("div");
for(var i=0;i<buttons.length;i++){
var div=document.createElement("div");
var cmd=document.createTextNode(buttons[i]);
div.eventDiv=i;
div.className="cmddiv";
div.appendChild(cmd);
contextMenu.appendChild(div);
}
contextMenu.className="contextMenu";
window.document.body.appendChild(contextMenu);
contextMenu.onclick=function(e){
var e = e||window.event;
var target=e.target||e.srcElement;
alert(target.eventDiv);
}
}
contextMenu.style.display="";
contextMenu.style.top=xy.y+10+"px";
contextMenu.style.left=xy.x+10+"px";
return false;
}
window.document.body.onclick=function(){
if(typeof contextMenu=="object"){
contextMenu.style.display="none";
}
}
var getXY=function(e){
var x,y;
var e = e||window.event;
return {x:e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,y:e.clientY+document.body.scrollTop+document.documentElement.scrollTop};
}
}
代码只写到判断出点击的是那个命令,至于要实现的函数,还是要自己写的
相关文档:
<html>
<head>
<mce:script type = "text/javascript"><!--
function changetext()
{
var x = document.getElementById("list");
for (var i = 0;i < x.length;i++ )
{
if (x.options[i].selected)
{
document.getElemen ......
<iframe width='100%' height='100%' name='boot' id='boot' src='' frameborder='0' scrolling='no'></iframe>
<SCRIPT LANGUAGE="JavaScript">
<!--
var iframe = window.frames['boot'];
iframe.document.open();
iframe.document.write('<!DOCTY ......
window.onbeforeunload = function (evt) {
var message = 'Are you sure you want to leave?';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}
onbeforeunload 事件参考地址
http://msdn.microsoft.com/en-us/ ......