使用jQuery显示隐藏内容

html5代码:

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>Document</title>
		<link rel="stylesheet" href="css/global(1).css" />
		<link rel="stylesheet" href="css/index.css" />
		<script src="lib/jquery-3.6.0.js"></script>
	</head>
	<body>
		<div>
			<ul>
				<li>
					<h1>林徽因简介</h1>
				</li>
				<li>
					林徽因,女,汉族,原名林微音,出生于浙江杭州, 祖籍福建福州。中国著名建筑家、诗人、作家。人民英雄 纪念碑和中华人民共和国国徽深化方案的设计者
					林徽因是建筑师梁思成的第一任妻子。三十年代初 与夫婿梁思成用现代科学方法研究方法研宄中国古代建 筑,成为这个学术领域的开拓者。
				</li>
				<li><h1>主要作品</h1></li>
				<li>
					<p>《你是人间四月天》</p>
					<p>《宝莲灯》</p>
					<p>《九十九度中》</p>
				</li>
			</ul>
		</div>
		<script src="js/index.js"></script>
	</body>
</html>

将已定义的内容隐藏:

div {
	width: 200px;
	margin: 30px auto;
}
ul li:nth-child(1) {
	background-color: #333333;
	color: #fff;
	height: 50px;
	cursor: pointer;
}
h1 {
	text-align: center;
	font-size: 25px;
	line-height: 50px;
}
ul li:nth-child(2) {
	line-height: 20px;
	font-weight: 700;
	display: none;
}
ul li:nth-child(3) {
	background-color: #333333;
	color: #fff;
	height: 50px;
	cursor: pointer;
}
ul li:nth-child(4) p {
	line-height: 20px;
	display: none;
}

jQuery代码将隐藏部分显示:

$(document).ready(function () {
	$("ul li:nth-child(1)").click(function () {
		$("ul li:nth-child(2)").css({ display: "block" });
		$("ul li:nth-child(4) p").css({ display: "none" });
	});
	$("ul li:nth-child(3)").click(function () {
		$("ul li:nth-child(4) p").css({ display: "block" });
		$("ul li:nth-child(2)").css({ display: "none" });
	});
});

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值