jQuery UI提供了两个强大的组件:对话框(Dialog)和选项卡(Tabs)。对话框是一个浮动窗口,可以包含内容、表单或交互元素;选项卡则允许在有限的空间内组织多个内容面板。将两者结合可以创建更复杂的用户界面。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery UI对话框与选项卡结合</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<style>
#dialog-container { display: none; }
</style>
</head>
<body>
<button id="open-dialog">打开对话框</button>
<div id="dialog-container" title="带选项卡的对话框">
<div id="tabs">
<ul>
<li><a href="#tab-1">选项卡1</a></li>
<li><a href="#tab-2">选项卡2</a></li>
<li><a href="#tab-3">选项卡3</a></li>
</ul>
<div id="tab-1">
<p>这是第一个选项卡的内容</p>
</div>
<div id="tab-2">
<p>这是第二个选项卡的内容</p>
</div>
<div id="tab-3">
<p>这是第三个选项卡的内容</p>
</div>
</div>
</div>
<script>
$(function() {
// 初始化选项卡
$("#tabs").tabs();
// 初始化对话框
$("#dialog-container").dialog({
autoOpen: false,
modal: true,
width: 500,
height: 300,
buttons: {
"确定": function() {
$(this).dialog("close");
},
"取消": function() {
$(this).dialog("close");
}
}
});
// 点击按钮打开对话框
$("#open-dialog").on("click", function() {
$("#dialog-container").dialog("open");
});
});
</script>
</body>
</html>
原因:通常是因为对话框初始化时选项卡还未完全加载或尺寸计算错误。
解决方案:
$("#dialog-container").dialog({
open: function() {
$(this).tabs("refresh"); // 刷新选项卡
}
});
解决方案:
$("#dialog-container").dialog({
width: "auto",
height: "auto"
});
解决方案:
// 添加新选项卡后
$("#tabs").tabs("refresh");
$("#dialog-container").dialog("option", "position", { my: "center", at: "center", of: window });
$("#tabs").tabs({
beforeLoad: function(event, ui) {
ui.panel.html("加载中...");
ui.jqXHR.fail(function() {
ui.panel.html("加载失败,请重试。");
});
}
});
$("#dialog-container").dialog({
buttons: {
"提交": function() {
// 获取当前活动选项卡中的表单数据
var activeTab = $("#tabs").tabs("option", "active");
var formData = $("#tabs div.ui-tabs-panel:eq(" + activeTab + ") form").serialize();
// 提交表单
$.post("submit.php", formData, function(response) {
// 处理响应
});
}
}
});
通过以上方法,你可以有效地将jQuery UI的对话框和选项卡组件结合起来,创建出功能丰富且用户友好的界面。
没有搜到相关的文章