javascript - 在 sweetalert JavaScript 中格式化字符串
问题描述
我有返回此字符串的 API:
string.Format(
"Something went wrong with {0}." +
"<br>" +
" Do you want to continue with?" +
"<strong>" +
"{1}" +
"</strong>",
);
我想把它传递给 SweetAlert2。但这是结果:
我也尝试了\r\n和\n但什么也没发生。
这是我的 JavaScript:
var mess = "@messaggio";
Swal.fire({
heightAuto: false,
title: "Richiesta Disconnessione",
html: mess,
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes",
cancelButtonText: "No",
});
我想把第二个短语放在第一个词下面,最后一个词应该是粗体。
任何建议如何解决这个问题?
提前致谢!
解决方案
您使用的是旧版本的 sweetalert
查看更多信息:https ://sweetalert.js.org/docs/
// First, checks if it isn't implemented yet.
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
var mess = "Something went wrong with {0}.\nDo you want to continue with {1}?".format("Macchina", "carrello");
swal(mess,{
heightAuto: false,
title: 'Richiesta Disconnessione',
icon: 'warning',
buttons: {
cancel: {
text: "No",
value: null,
visible: true,
className: "sweet-cancel",
closeModal: true,
},
confirm: {
text: "Yes",
value: true,
visible: true,
className: "sweet-confirm",
closeModal: true
}
}
})
.sweet-confirm { color: #3085d6; }
.sweet-cancel { color: #d33; }
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
推荐阅读
- android - 您如何使用带数字的点指示器创建视图寻呼机?
- gcc - 令人困惑的内联汇编
- c# - 如何通过 COM 将 SAFEARRAY 从 C++ 传递到 C#
- jsf - JSF 2.x 将复杂视图简化为单个标记
- reactjs - 使子组件重新渲染而不发送无用的道具
- java - Java HttpSessionListener:如何自定义不同类型的会话?
- python - Python - 网页抓取 - 分页卡在第 1 页,没有进一步进展
- azure - 我们可以根据连接数自动扩展无状态服务吗
- c - 在 C 中保存数据并在 Python 中加载
- asp.net - 在 asp.net core 中使用soap web服务