WEB前端:03_hover切换(显示/隐藏)

hover切换(显示/隐藏)

 

网站常用效果之一,以下为简化版,用于学习javascript基础知识。

 

效果图:

 

hover显示/隐藏切换 - 纯JS简化版

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>hover显示/隐藏切换 - 纯JS简化版</title>
<style type="text/css">

*{margin: 0; padding: 0;}

#hoverbox {margin:10px; width: 500px;}
#hovertit {background: #666; padding: 10px;color: #fff;}
#hoverdiv { display: none; border: 1px solid #666; padding: 10px;}

</style>
<script type="text/javascript">
window.onload = function() {
	
	var hovertit = document.getElementById('hovertit');
	var hoverdiv = document.getElementById('hoverdiv');

	hovertit.onclick = function() {
		//hoverdiv.style.display = hoverdiv.style.display == 'block' ? 'none' : 'block';
		if(hoverdiv.style.display == 'block') {
			hoverdiv.style.display = 'none';
		} else {
			hoverdiv.style.display = 'block';
		}
	}
}
</script>
</head>
<body>

<div class="hoverbox">
	<div id="hovertit">小标题一</div>
	<div id="hoverdiv">小标题内容一</div>
</div>

</body>
</html>

 

hover显示/隐藏切换[版本二]

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>hover显示/隐藏切换 - 纯JS简化版</title>
<style type="text/css">

*{margin: 0; padding: 0;}

.hoverbox {margin:10px; width: 500px;}
.hovertit {background: #666; padding: 10px;color: #fff;}
.hoverdiv { display: none; border: 1px solid #666; padding: 10px;}

</style>
<script type="text/javascript">
window.onload = function() {
	
	function getClass(elem, elements) {
		var tags = elem.getElementsByTagName('*');
		var arr = [];
		for(var i=0; i<tags.length; i++) {
			if(tags[i].className == elements) {
				arr.push(tags[i]);
			}
		}
		return arr;
	}

	var hoverbox = getClass(document, 'hoverbox');

	for(var b=0; b<hoverbox.length; b++) {
		getClass(hoverbox[b], 'hovertit')[0].index = b;
		getClass(hoverbox[b], 'hovertit')[0].onclick = function() {
			if(getClass(hoverbox[this.index], 'hoverdiv')[0].style.display == 'block') {
				getClass(hoverbox[this.index], 'hoverdiv')[0].style.display = 'none';
			} else {
				getClass(hoverbox[this.index], 'hoverdiv')[0].style.display = 'block'
			}
			//getClass(hoverbox[this.index], 'hoverdiv')[0].style.display = getClass(hoverbox[this.index], 'hoverdiv')[0].style.display == 'block' ? 'none' : 'block';
		}
	}

}
</script>
</head>
<body>

<div class="hoverbox">
	<div class="hovertit">小标题一</div>
	<div class="hoverdiv">小标题内容一</div>
</div>

<div class="hoverbox">
	<div class="hovertit">小标题二</div>
	<div class="hoverdiv">小标题内容二</div>
</div>

</body>
</html>

 

转载于:https://www.cnblogs.com/haicheng/p/3703720.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值