多级菜单

HTML与JavaScript上下文菜单
本文介绍了一个使用HTML和JavaScript实现的上下文菜单示例。该菜单通过鼠标右键点击显示,并采用绝对定位来确定其出现的位置。文章还展示了如何通过鼠标悬停效果展示和隐藏子菜单。

<!DOCTYPE html>

<html>


<head>

<meta charset="UTF-8">

<title></title>

<style type="text/css">

* {

margin: 0;

padding: 0;

}

#ul1 {

position: absolute;

display: none;

}

ul {

list-style: none;

}

li {

background-color: paleturquoise;

color: deeppink;

width: 100px;

text-align: center;

position: relative;

font-size: 12px;

}

li>ul {

width: 100px;

position: absolute;

left: 98px;

top: -2px;

display: none;

}

</style>

</head>


<body>

<ul id="ul1">

<li>我是li1</li>

<li>我是li2</li>

<li>我是li3

<ul>

<li>我是li3下的li1</li>

<li>我是li3下的li2</li>

<li>我是li3下的li3</li>

</ul>

</li>

<li>我是li4

<ul>

<li>我是li4下的li1</li>

<li>我是li4下的li2

<ul>

<li>我是li4下的li2下的li1</li>

<li>我是li4下的li2下的li2</li>

<li>我是li4下的li2下的li3</li>

</ul>

</li>

<li>我是li4下的li3</li>

</ul>

</li>

<li>我是li5</li>

</ul>

<script type="text/javascript">

var ul1 = document.getElementById("ul1");

var lis = document.getElementsByTagName("li");

document.oncontextmenu = function() {

ul1.style.display = "block";

var x = event.clientX;

var y = event.clientY;

ul1.style.left = x + "px";

ul1.style.top = y + "px";

return false;

}

document.onclick = function() {

ul1.style.display = "none";

}

for (var i = 0; i < lis.length; i++) {

lis[i].onmouseover = function(){

var child = this.children;

// li有孩子节点

if (child.length > 0) {

// 过滤其他标签

if (child[0].nodeName == "UL") {

child[0].style.display = "block";

}

}

}

lis[i].onmouseout = function(){

var child = this.children;

// li有孩子节点

if (child.length > 0) {

// 过滤其他标签

if (child[0].nodeName == "UL") {

child[0].style.display = "none";

}

}

}

}

</script>

</body>


</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值