思路:
你需要先组织默认的右键事件,
你需要自己制作一个右键图层,
下面是简单的代码:
<style>* {margin:0 padding:0 list-style:none}
#ul1 li {width:400px background:#CCC border:1px solid black margin:10px float:left line-height:30px text-indent:1em}
#menu {width:150px border:1px solid black border:1px solid black position:absolute left:0 top:0 display:none background:white}
#menu li {padding:3px}
#menu li:hover {background:#CCC}
</style>
<script>
window.onload=function (){
var aLi=document.getElementById('ul1').children
var oMenu=document.getElementById('menu')
var aMenuLi=oMenu.children
for (var i=0 i<aLi.length i++)
{
aLi[i].oncontextmenu=function (ev){
var oEvent=ev || event
oMenu.style.display='block'
oMenu.style.left=oEvent.clientX+'px'
oMenu.style.top=oEvent.clientY+'px'
var _this=this
aMenuLi[0].onclick=function (){
_this.parentNode.removeChild(_this)
}
return false
}
}
document.onclick=function (){
oMenu.style.display='none'
}
}
</script>
</head>
<body>
<ul id="menu">
<li>删除</li>
<li>变成红色</li>
<li>变成绿色</li>
<li>变成黄色</li>
<li>变成蓝色</li>
</ul>
<ul id="ul1">
<li>中国信保原副总戴春宁贪污、受贿、通奸被开除党籍</li>
<li>中纪委原书记贺国强考察贵阳孔学堂(图)</li>
<li>解放军报官微:胡锡进相关微博内容是胡说八道</li>
<li>刘汉向湖北省高院提出上诉 已提交上诉状</li>
<li>澳洲公布疑似马航坠毁噪音 或解MH370失踪之谜</li>
<li>曝京沪高铁上海段地面开裂10cm 回应称不影响运营</li>
<li>朝鲜宣传一农场已建成“社会主义世外桃源”(图)</li>
</ul>
</body>
你这个效果的下拉菜单,不需要用 JavaScript,只用CSS就能实现。
加上一句
li:hover ul{ display:block}
就行了。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8"/>
<title>无标题文档</title>
<style type="text/css">* {
margin: 0px
padding: 0px
}
body {
font-family: Verdana, Geneva, sans-serif
font-size: 14px
}
#nav {
width: 600px
height: 40px
background-color: #eee
margin: 0 auto
}
ul {
list-style: none
}
ul li {
float: left
line-height: 40px
text-align: center
width: 100px
}
a {
text-decoration: none
color: #000
display: block
}
a:hover {
color: #F00
background-color: #666
}
ul li ul li {
float: none
background-color: #eee
margin: 2px 0px
}
ul li ul {
display: none
}
li:hover ul{ display:block}
</style>
</head>
<body>
<div id="nav">
<ul>
<li><a href="#">首页</a></li>
<li><a href="#">课程大厅</a>
<ul>
<li><a href="#">JavaScript</a></li>
<li><a href="#">Html/CSS</a></li>
</ul>
</li>
<li><a href="#">学习中心</a>
<ul>
<li><a href="#">视频学习</a></li>
<li><a href="#">实例练习</a></li>
<li><a href="#">问与答</a></li>
</ul>
</li>
<li><a href="#">经典案例</a></li>
<li><a href="#">关于我们</a></li>
</ul>
</div>
</body>
</html>
下拉菜单关键在于HTML的布局和CSS的样式写法。这有这两个写好了后,js操作起来才非常的简单。下面是简单例子,仅供参考:<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head><meta http-equiv="Content-Type" content="text/html charset=gb2312" />
<title>js菜单演示_怎么使用js_js下拉菜单_简单js下拉菜单_怎么做js下拉菜单</title>
<style type="text/css">
*{margin:0padding:0border:0}
body {font-family: arial, 宋体, seriffont-size:12px}
#nav {line-height: 24px list-style-type: none background:#666}
#nav a {display: block width: 80px text-align:center}
#nav a:link {color:#666 text-decoration:none}
#nav a:visited {color:#666text-decoration:none}
#nav a:hover {color:#FFFtext-decoration:nonefont-weight:bold}
#nav li {float: left width: 80px background:#CCC}
#nav li a:hover{background:#999}
#nav li ul {line-height: 27px list-style-type: nonetext-align:leftleft: -999em width: 180px position: absolute}
#nav li ul li{float: left width: 120pxbackground: #F6F6F6}
#nav li ul a{display: block width: 100pxtext-align:leftpadding-left:20px}
#nav li ul a:link {color:#666 text-decoration:none}
#nav li ul a:visited {color:#666text-decoration:none}
#nav li ul a:hover {color:#F3F3F3text-decoration:nonefont-weight:normalbackground:#C00}
#nav li:hover ul {left: auto}
#nav li.onhover ul {left: auto}
#content {clear: left}
</style>
<script type="text/javascript">
function menu() {
var oLi = document.getElementById("nav").getElementsByTagName("li")
for (var i=0 i<oLi.length i++) {
oLi[i].onmouseover=function() {
this.className+=(this.className.length>0? " ": "") + "onhover"
}
oLi[i].onMouseDown=function() {
this.className+=(this.className.length>0? " ": "") + "onhover"
}
oLi[i].onMouseUp=function() {
this.className+=(this.className.length>0? " ": "") + "onhover"
}
oLi[i].onmouseout=function() {
this.className=this.className.replace(new RegExp("( ?|^)onhover\b"),"" )}}}
window.onload=menu
</script></head>
<body>
<ul id="nav">
<li><a href="http://jingyan.baidu.com/article/fc07f98917a56f12ffe5198a.html" target="_blank" >动漫之家</a>
<ul>
<li>
<a href="http://jingyan.baidu.com/article/363872ecedf4486e4ba16f92.html" target="_blank" >下拉一</a></li>
<li><a href="http://jingyan.baidu.com/article/363872ecedf4486e4ba16f92.html" target="_blank" >下拉二</a></li>
<li><a href="http://jingyan.baidu.com/article/363872ecedf4486e4ba16f92.html" target="_blank" >下拉三</a></li>
<li><a href="http://jingyan.baidu.com/article/363872ecedf4486e4ba16f92.html" target="_blank" >下拉四</a></li>
</ul></li><li><a href="http://jingyan.baidu.com/article/e73e26c0cfa01724adb6a795.html" target="_blank" >js详解</a><ul>
<li><a href="http://jingyan.baidu.com/article/e73e26c0cfa01724adb6a795.html" target="_blank" >下拉一</a></li>
<li><a href="http://jingyan.baidu.com/article/e73e26c0cfa01724adb6a795.html" target="_blank" >下拉二</a></li>
<li><a href="http://jingyan.baidu.com/article/e73e26c0cfa01724adb6a795.html" target="_blank" >下拉三</a></li>
<li><a href="http://jingyan.baidu.com/article/e73e26c0cfa01724adb6a795.html" target="_blank" >下拉四</a></li></ul></li>
<li><a href="http://jingyan.baidu.com/article/08b6a591f09cfc14a8092293.html" target="_blank" >js运用</a>
<ul><li><a href="http://jingyan.baidu.com/article/08b6a591f09cfc14a8092293.html" target="_blank" >下拉一</a></li>
<li><a href="http://jingyan.baidu.com/article/08b6a591f09cfc14a8092293.html" target="_blank" >下拉二</a></li>
<li><a href="http://jingyan.baidu.com/article/08b6a591f09cfc14a8092293.html" target="_blank" >下拉三</a></li>
<li><a href="http://jingyan.baidu.com/article/08b6a591f09cfc14a8092293.html" target="_blank" >下拉四</a></li></ul></li>
<li><a href="http://jingyan.baidu.com/article/363872ecedf4486e4ba16f92.html" target="_blank" >关于我们</a>
<ul><li><a href="http://jingyan.baidu.com/article/363872ecedf4486e4ba16f92.html" target="_blank" >下拉一</a></li>
<li><a href="http://jingyan.baidu.com/article/363872ecedf4486e4ba16f92.html" target="_blank" >下拉二</a></li>
<li><a href="http://jingyan.baidu.com/article/363872ecedf4486e4ba16f92.html" target="_blank" >下拉三</a></li>
<li><a href="http://jingyan.baidu.com/article/363872ecedf4486e4ba16f92.html" target="_blank" >下拉四</a></li>
</ul></li><li><a href="http://jingyan.baidu.com/article/cdddd41c5e873153cb00e193.html" target="_blank" >js在线演示</a>
<ul><li><a href="http://jingyan.baidu.com/article/cdddd41c5e873153cb00e193.html" target="_blank" >下拉一</a></li>
<li><a href="http://jingyan.baidu.com/article/cdddd41c5e873153cb00e193.html" target="_blank" >下拉二</a></li>
<li><a href="http://jingyan.baidu.com/article/cdddd41c5e873153cb00e193.html" target="_blank" >下拉三</a></li>
<li><a href="http://jingyan.baidu.com/article/cdddd41c5e873153cb00e193.html" target="_blank" >下拉四</a></li></ul></li>
<li><a href="http://jingyan.baidu.com/article/76a7e409bb3c33fc3b6e1593.html" target="_blank" >联系我们</a>
<ul><li><a href="http://jingyan.baidu.com/article/76a7e409bb3c33fc3b6e1593.html" target="_blank" >下拉一</a></li>
<li><a href="http://jingyan.baidu.com/article/76a7e409bb3c33fc3b6e1593.html" target="_blank" >下拉二</a></li>
<li><a href="http://jingyan.baidu.com/article/76a7e409bb3c33fc3b6e1593.html" target="_blank" >下拉三</a></li>
<li><a href="http://jingyan.baidu.com/article/76a7e409bb3c33fc3b6e1593.html" target="_blank" >下拉四</a></li></li></ul>
</body>
</html>