ES5与ES6面向对象,写选项卡

这篇博客展示了如何使用ES5和ES6来实现HTML导航菜单与内容区域的交互效果。通过对比两种JavaScript语法,解释了如何在鼠标悬停时改变选卡的样式和显示相应的内容。示例中,选卡和内容通过CSS进行布局,而JavaScript处理了高亮显示和内容切换的逻辑。
摘要由CSDN通过智能技术生成

ES5:

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>Title</title>
		<style>
			* {
				text-align: center;
			}

			nav li {
				display: inline;
				width: 24%;
				text-decoration: none;
				padding: 15px;
			}

			li:hover {
				background-color: lightblue;
			}

			section {
				height: 300px;
			}
			.tab{
				display: block;
			}
			 section {
				display: none;
			}
		</style>
	</head>
	<body>
		<nav id="nav">
			<ul>
				<li >选卡1</li>
				<li>选卡2</li>
				<li>选卡3</li>
				<li>选卡4</li>
			</ul>
		</nav>
		<div id="container">
			<section  class="tab" style="background-color: tomato;">内容1</section>
			<section  style="background-color: cyan">内容2</section>
			<section  style="background-color: blueviolet">内容3</section>
			<section  style="background-color: gold">内容4</section>
		</div>
		<script>
			window.onload = function() {
				var li=document.getElementsByTagName('li');
				var sec=document.getElementsByTagName('section');
				console.log(li);
				for (var i = 0; i < li.length; i++) {
					li[i].index=i;
					li[i].onmouseover=function  () {
						for (var i = 0; i < li.length; i++) {
							li[i].className='';
							sec[i].className='';
						}
						sec[this.index].className='tab';
					}
				}
			};
		</script>
	</body>
</html>

ES6:

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>Title</title>
		<style>
			* {
				text-align: center;
			}

			li {
				display: inline;
				width: 24%;
				text-decoration: none;
				padding: 15px;
			}

			li:hover {
				background-color: lightblue;
			}

			section {
				height: 300px;
			}

			.tab {
				display: block;
			}

			section {
				display: none;
			}
		</style>
	</head>
	<body>
		<div class="box">


			<ul>
				<li id="li">选卡1</li>
				<li>选卡2</li>
				<li>选卡3</li>
				<li>选卡4</li>
			</ul>

			<div id="container">
				<section class="tab" style="background-color: tomato;">内容1</section>
				<section style="background-color: cyan">内容2</section>
				<section style="background-color: blueviolet">内容3</section>
				<section style="background-color: gold">内容4</section>
			</div>
		</div>
		<script>
			var li = document.getElementById('li');
			

			function Pander(sed) {
				this.sed = document.querySelector(sed)
				this.btn = this.sed.querySelectorAll('ul>li')
				this.sec = this.sed.querySelectorAll('div>section')
				this.change()
			}

			Pander.prototype.change = function() {
				this.btn.forEach((item, index) => {
					item.addEventListener('mouseover', () => {
						this.btn.forEach((t, i) => {
							t.className = ''
							this.sec[i].className = ''
						})
						this.sec[index].className = 'tab'
					})
				})
			}
			let d1 = new Pander('.box')
		</script>
	</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

似琼碧落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值