SweetAlert 文本格式
SweetAlert text formatting
我正在使用 sweetAlert 来显示对话框。在我的对话框中,我必须显示一个大字符串,中间有换行符。我的示例字符串如下:
var str="Task1Name : Success : statusCode\n
Task2NameLonger : Failed : statusCode\n"
等等。所以,基本上,我希望它们中的每一个都换行并且空格匹配。当我使用 sweetalert 对话框时,换行符正确显示,但文本自动对齐到中心并且间距被截断。谁能帮我手动设置对齐方式和间距?
将字符串包装到 <pre>
标记中可能是一种解决方案:
var str="Task1Name : Success : statusCode\n" +
"Task2NameLonger : Failed : statusCode\n";
Swal.fire({
html: '<pre>' + str + '</pre>',
customClass: {
popup: 'format-pre'
}
});
.format-pre pre {
background: #49483e;
color: #f7f7f7;
padding: 10px;
font-size: 14px;
}
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
PS。原版sweet alert插件不支持,建议使用SweetAlert2插件
迁移很简单,这里是迁移指南:Migration from SweetAlert to SweetAlert2
如果切换到 sweetalert2,可以使用 html 代替文本:
swal({ title: 'hi', html: 'First line<br>Second line' });
我还必须使用 swal(2) 显示(很多)文本,并希望显示大部分文本 左 对齐并且最后一行居中。
首先,我尝试用 customClass: 'swal-breit',
应用 css
并在 css.
中设置宽度和对齐方式
但这对我的情况不起作用...
由于 css 似乎不适用于我的情况,我已使用 HTML(段落和对齐) 实现了它。在我的(juery)函数(带有嵌入式 swal)中,我首先使用 html 代码创建一个 变量 ,然后设置 属性 html:
到变量。
简短的介绍:
首先显示大量文本(左对齐),然后显示一个问题(居中)。
如果用户单击 'Ja'(是),将加载特定页面(视图),如果他单击 'Nein'(否),则什么也不会发生(swal ist 关闭,用户留在页面(视图)上.
代码片段(此处简化为 post)(德语):
function MySwalFunction() {
var Meldungstext = "";
Meldungstext = Meldungstext + '<p align="left"> text in paragraph here.. <strong>strong text kere...</strong> further normal text with line break. <br>'
Meldungstext = Meldungstext + 'Second line of text </p>'
Meldungstext = Meldungstext + '<p align="left"> second paragraph block.. with linebreak <br>'
Meldungstext = Meldungstext + 'second line to second block </p>'
Meldungstext = Meldungstext + '<p align="left">tex ti third paragraph here </p>'
Meldungstext = Meldungstext + '<strong>Last line WITHOUT paragraph = is showed centered (swal default) </strong>'
swal.fire({
title: '! Achtung !',
html: Meldungstext,
icon: 'question',
showCancelButton: true,
width: '90%',
confirmButtonColor: 'blue',
cancelButtonText: 'Nein',
confirmButtonText: 'Ja',
}).then(function (result) {
if (result.value) {
var hostname = window.location.origin;
var xlink = hostname + "/main/sub/"
location.href = xlink;
}
else {
}
})
}
您可以只使用 \n 换行
我正在使用 sweetAlert 来显示对话框。在我的对话框中,我必须显示一个大字符串,中间有换行符。我的示例字符串如下:
var str="Task1Name : Success : statusCode\n
Task2NameLonger : Failed : statusCode\n"
等等。所以,基本上,我希望它们中的每一个都换行并且空格匹配。当我使用 sweetalert 对话框时,换行符正确显示,但文本自动对齐到中心并且间距被截断。谁能帮我手动设置对齐方式和间距?
将字符串包装到 <pre>
标记中可能是一种解决方案:
var str="Task1Name : Success : statusCode\n" +
"Task2NameLonger : Failed : statusCode\n";
Swal.fire({
html: '<pre>' + str + '</pre>',
customClass: {
popup: 'format-pre'
}
});
.format-pre pre {
background: #49483e;
color: #f7f7f7;
padding: 10px;
font-size: 14px;
}
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
PS。原版sweet alert插件不支持,建议使用SweetAlert2插件
迁移很简单,这里是迁移指南:Migration from SweetAlert to SweetAlert2
如果切换到 sweetalert2,可以使用 html 代替文本:
swal({ title: 'hi', html: 'First line<br>Second line' });
我还必须使用 swal(2) 显示(很多)文本,并希望显示大部分文本 左 对齐并且最后一行居中。
首先,我尝试用 customClass: 'swal-breit',
应用 css
并在 css.
中设置宽度和对齐方式
但这对我的情况不起作用...
由于 css 似乎不适用于我的情况,我已使用 HTML(段落和对齐) 实现了它。在我的(juery)函数(带有嵌入式 swal)中,我首先使用 html 代码创建一个 变量 ,然后设置 属性 html:
到变量。
简短的介绍:
首先显示大量文本(左对齐),然后显示一个问题(居中)。
如果用户单击 'Ja'(是),将加载特定页面(视图),如果他单击 'Nein'(否),则什么也不会发生(swal ist 关闭,用户留在页面(视图)上.
代码片段(此处简化为 post)(德语):
function MySwalFunction() {
var Meldungstext = "";
Meldungstext = Meldungstext + '<p align="left"> text in paragraph here.. <strong>strong text kere...</strong> further normal text with line break. <br>'
Meldungstext = Meldungstext + 'Second line of text </p>'
Meldungstext = Meldungstext + '<p align="left"> second paragraph block.. with linebreak <br>'
Meldungstext = Meldungstext + 'second line to second block </p>'
Meldungstext = Meldungstext + '<p align="left">tex ti third paragraph here </p>'
Meldungstext = Meldungstext + '<strong>Last line WITHOUT paragraph = is showed centered (swal default) </strong>'
swal.fire({
title: '! Achtung !',
html: Meldungstext,
icon: 'question',
showCancelButton: true,
width: '90%',
confirmButtonColor: 'blue',
cancelButtonText: 'Nein',
confirmButtonText: 'Ja',
}).then(function (result) {
if (result.value) {
var hostname = window.location.origin;
var xlink = hostname + "/main/sub/"
location.href = xlink;
}
else {
}
})
}
您可以只使用 \n 换行