jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。漂亮对话框(通常指模态对话框)是一种用户界面元素,它在当前页面上显示一个覆盖层,阻止用户与底层页面交互,直到对话框被关闭。
以下是一个使用 jQuery 和 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 Dialog Example</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>
#dialog {
display: none;
}
</style>
</head>
<body>
<div id="dialog" title="Basic dialog">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>
<button id="open-dialog">Open Dialog</button>
<script>
$(function() {
$("#dialog").dialog({
autoOpen: false,
modal: true
});
$("#open-dialog").click(function() {
$("#dialog").dialog("open");
});
});
</script>
</body>
</html>
display
属性设置为 none
,以便在初始化时隐藏。modal
属性设置为 true
。通过以上步骤,你可以创建一个功能齐全且美观的 jQuery 对话框,并解决常见的显示和交互问题。
领取专属 10元无门槛券
手把手带您无忧上云