对话框(Dialog)是一种用户界面元素,用于向用户显示信息或获取用户的输入。jQuery 是一个流行的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。
以下是一个使用 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>
<script>
$(function() {
$("#dialog").dialog({
autoOpen: false,
modal: true,
buttons: {
"OK": function() {
$(this).dialog("close");
}
}
});
$("#openDialog").click(function() {
$("#dialog").dialog("open");
});
});
</script>
</head>
<body>
<button id="openDialog">Open Dialog</button>
<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>
</body>
</html>
原因:
解决方法:
$(function() {
$("#dialog").dialog({
autoOpen: false,
modal: true,
buttons: {
"OK": function() {
$(this).dialog("close");
}
}
});
$("#openDialog").click(function() {
$("#dialog").dialog("open");
});
});
通过以上步骤,可以确保对话框能够正确打开并显示。
领取专属 10元无门槛券
手把手带您无忧上云