jQuery UI 是一个基于 jQuery 的 Web 应用程序用户界面库,提供了丰富的 UI 组件和效果。手风琴效果(Accordion Effect)是其中之一,它允许用户在一个容器中展开和折叠多个面板,每个面板包含不同的内容。
jQuery UI 手风琴效果主要分为以下几种类型:
以下是一个简单的 jQuery UI 多级手风琴效果的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery UI Accordion</title>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<style>
.accordion-container {
width: 60%;
margin: 20px auto;
}
.accordion-panel {
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="accordion-container">
<div id="accordion">
<h3>Section 1</h3>
<div>
<p>Content for section 1.</p>
<div class="accordion-panel">
<h4>Subsection 1.1</h4>
<div>
<p>Content for subsection 1.1.</p>
</div>
</div>
</div>
<h3>Section 2</h3>
<div>
<p>Content for section 2.</p>
</div>
<h3>Section 3</h3>
<div>
<p>Content for section 3.</p>
</div>
</div>
</div>
<script>
$(function() {
$("#accordion").accordion({
collapsible: true,
active: false
});
});
</script>
</body>
</html>
原因:
解决方法:
<h3>
和 <div>
标签的嵌套关系。例如,确保以下代码正确:
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
$(function() {
$("#accordion").accordion({
collapsible: true,
active: false
});
});
通过以上步骤,可以解决手风琴效果无法正常展开和折叠的问题。
没有搜到相关的文章