jQuery 伸缩选项卡(Accordion Tabs)是一种常见的网页交互元素,允许用户通过点击或悬停来展开和折叠内容区域。每个选项卡通常包含一个标题和一个内容区域,用户可以点击标题来显示或隐藏相应的内容。
以下是一个简单的 jQuery 伸缩选项卡的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Accordion Tabs</title>
<style>
.accordion {
width: 100%;
max-width: 400px;
margin: 20px auto;
}
.accordion h3 {
background-color: #f1f1f1;
padding: 10px;
cursor: pointer;
}
.accordion .content {
background-color: #fff;
padding: 10px;
display: none;
}
</style>
</head>
<body>
<div class="accordion">
<h3>Tab 1</h3>
<div class="content">
<p>Content for Tab 1</p>
</div>
<h3>Tab 2</h3>
<div class="content">
<p>Content for Tab 2</p>
</div>
<h3>Tab 3</h3>
<div class="content">
<p>Content for Tab 3</p>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('.accordion h3').click(function() {
$(this).next('.content').slideToggle('fast');
$(this).siblings('h3').next('.content').slideUp('fast');
});
});
</script>
</body>
</html>
通过以上示例代码和常见问题解决方法,您可以轻松实现一个基本的 jQuery 伸缩选项卡功能。如果需要更复杂的功能或样式,可以根据需求进行扩展和调整。
没有搜到相关的文章