简体   繁体   English

有没有办法在 Javascript 中显示多行字符串?

[英]Is there a way to display multi-line string in Javascript?

I have a function which receives a single line string and displays it without a problem using an alert pop up from alertify JS but when its a multi-line string it gives an error saying Uncaught SyntaxError: Invalid or unexpected token .我有一个 function,它接收单行字符串并使用 alertify JS 弹出的警报毫无问题地显示它,但是当它是一个多行字符串时,它给出一个错误说Uncaught SyntaxError: Invalid or unexpected token Below is my function:下面是我的function:

<script>
function changes(changes) {

    alertify.confirm(changes,
        function () {
        }).setHeader('Document Changes');
}

The changes is the string am receiving and am displaying it in an alert box.更改是正在接收的字符串,并将其显示在警告框中。 Below is where am getting the changes from.下面是我从哪里得到的变化。 I am getting this string from an object and passing it to my function我从 object 获取这个字符串并将其传递给我的 function

                {
                "render": function (data, type, full, meta) {
                    return '<button onclick="changes(\'' + full.changes + '\')" class="btn btn-info"><i class="fas fa-info-circle"></i> Changes</button>';
                }
            }

When i click this button it does not send the multi-line string to my function but when its a single line string it works without a problem当我单击此按钮时,它不会将多行字符串发送到我的 function,但是当它是单行字符串时,它可以正常工作

The sample text which brings the error is like below:带来错误的示例文本如下所示:

added footer添加页脚

added heder添加了heder

added content添加的内容

Below is a fiddle which i have replicated the error Js Fiddle下面是一个小提琴,我已经复制了错误Js Fiddle

if the problem was multi lines strings, you can use the back ticks ` to print the multi line strings in javascript如果问题是多行字符串,您可以使用反引号 ` 打印 javascript 中的多行字符串

 alert(`this is \n multi line \n string`)

and in your case would be:在你的情况下是:

alertify.confirm(`${changes}`,....

Use the backticks, Luke.使用反引号,卢克。

 let changes =` This is a multiline alert message `; alert(changes);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM