今天听了老师的课程,定制了一个右键菜单,写了个小例子供新手学习.
右键菜单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#context{
width:200px;
height:auto;
background:#ccc;
display:none;
position:absolute;
}
ul{
padding:0px;
margin:0px;
}
#context ul li{
width:100%;
height:35px;
border-bottom:1px solid #fff;
text-align:center;
line-height:35px;
list-style:none;
padding:0px;
}
a{
text-decoration:none;
color:#f00;
}
</style>
</head>
<body>
<div id='context'>
<ul>
<li><a href="">刷新</a></li>
<li><a href="b.php">联系我们</a></li>
</ul>
</div>
</body>
<script>
var obj = document.getElementById('context');
document.οncοntextmenu=function(event)
{
event.preventDefault();
var x = event.clientX;
var y = event.clientY;
obj.style.display = 'block';
obj.style.left = x+20+'px';
obj.style.top = y+20+'px';
}
var liobj=document.getElementsByTagName('li');
for(var i=0;i<liobj.length;i++)
{
liobj[i].οnmοuseοver=function(){
var aobj=this.getElementsByTagName('a');
this.style.background='#f00';
aobj[0].style.color="#000";
}
liobj[i].οnmοuseοut=function(){
var aobj=this.getElementsByTagName('a');
this.style.background='#ccc';
aobj[0].style.color="#f00";
}
}
document.οnclick=function(event)
{
if(event.button!=2)
{
obj.style.display = 'none';
}
}
</script>
</html>