我能否在JavaScript中编写自定义确认框, 以显示“保存”和“删除”按钮,而不是默认的“确认”和“取消”按钮?
我能否在JavaScript中编写自定义确认框, 以显示“保存”和“删除”按钮,而不是默认的“确认”和“取消”按钮?
使用jQuery UI对话框来完成此操作。
您可以像这样做:
JS:
$(function() {
$("#dialog-confirm").dialog({
resizable: false,
height: "auto",
width: 400,
modal: true,
buttons: {
"Do it": function() {
$(this).dialog("close");
},
Cancel: function() {
$(this).dialog("close");
}
}
});
});
<link href="https://jqueryui.com/jquery-wp-content/themes/jquery/css/base.css" rel="stylesheet"/>
<link href="http://jqueryui.com/jquery-wp-content/themes/jqueryui.com/style.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div id="dialog-confirm" title="Is it treason, then?">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:12px 12px 20px 0;"></span>Am I the Senate?</p>
</div>
$('confirm text').dialog(
{
modal:true, //Not necessary but dims the page background
buttons:{
'Save':function() {
//Whatever code you want to run when the user clicks save goes here
},
'Delete':function() {
//Code for delete goes here
}
}
}
);
confirm()
、alert()
或prompt()
命令,因为它们会阻塞整个浏览器线程(在大多数浏览器中)。你应该使用JavaScript-UI替代方案。 - jwueller