自定义JavaScript提示和确认框
Customize javascript prompt and confirmation box
我对HTML设计很陌生。我的前端是Angular.js。在我的应用程序中,执行了一些方法并提示用户输入。用户输入后,它继续执行其余逻辑。
目前,我正在使用简单的confirm()
或prompt()
。但是我需要有html模板而不是典型的消息。我尝试了如下内容。
var htmlStr = "<div> This is nice html with <h1>header</h1> and body content </div>";
var action = prompt(htmlStr);
它显示为字符串而不是 html。
知道如何转换为 html?欢迎任何帮助/建议。
这
是不可能的。在prompt
、alert
或confirm
弹出窗口中修改文本的范围仅限于向字符串中添加'n
字符以添加分隔线。(可能是其他一些转义字符,但不允许使用 html。这些弹出窗口的样式完全取决于浏览器。
window.prompt()
您可以做的是使用根据自己的喜好设置样式的position:absolute
或relative
div创建自定义模态。
您可以使用 sweetalert 显示自定义 JavaScript 提示或确认框。
swal({
title: "An input!",
text: "Write something interesting:",
type: "input", showCancelButton: true,
closeOnConfirm: false,
animation: "slide-from-top",
inputPlaceholder: "Write something" },
function (inputValue) {
if(inputValue === false) {
return false;
}
if(inputValue === "") {
swal.showInputError("You need to write something!");
return false;
}
swal("Nice!", "You wrote: " + inputValue, "success");
}
);
演示 https://jsfiddle.net/szf1jgog/
document.querySelector('button').onclick = function() {
swal({
title: "An input!",
text: 'Write something interesting:',
type: 'input',
showCancelButton: true,
closeOnConfirm: false,
animation: "slide-from-top",
inputPlaceholder: "Write something",
},
function(inputValue) {
if (inputValue === false) return false;
if (inputValue === "") {
swal.showInputError("You need to write something!");
return false;
}
swal("Nice!", 'You wrote: ' + inputValue, "success");
});
};
<link href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
<button>Alert me</button>
提示方法仅接受字符串作为其参数,因此,您传入的任何html
代码也将仅被视为字符串。如果你的项目是用angularjs的,那么我建议你看看angular-ui Bootstrap
这为所有常见用例提供了本机角度指令。可以使用modal
获取用户输入并根据需要设置其样式。
相关文章:
- 提示使用服务器端事件处理程序激活JavaScript
- d3.js Chord图的动态工具提示
- 是否可以用变量确认提示警报
- 确认消息没有提示
- 自定义JavaScript提示和确认框
- 窗口警报并确认工作,但不提示
- Javascript、提示框和确认框
- PHP执行前的确认提示
- jquery即兴确认/提示替换
- 如何在确认密码的工具提示中显示错误信息
- 是否有可能在标准的JavaScript提示/确认弹出框中删除键盘访问?
- 提示用户确认提交,不使用ajax
- 温馨提示,确认后继续提交表格
- 提示一个删除弹出窗口以确认删除- gridviewasp.net
- 单击链接时提示用户确认
- 在执行cutsom CbuttonColumn操作之前提示确认
- 获取浏览器提示"确认导航"选择“离开此页面”;或者“留在这一页”;在Woocommerce结账
- 确认提示信息(ASP.NET/VB)
- 提示,警报和确认JavaScript在Android WebView中找不到
- 在Safari, iOS上使用历史API后,警报,确认和提示不起作用