jquery / javascript中的换行符

时间:2014-07-05 10:07:56

标签: javascript jquery

现在我正在使用java脚本,jquery等。我面临的问题很少。

我在CASE块中写了一条短信,显示时它是一个很长的字符串:

Record has been saved successfully Note: If head already exists for the concerned department than your head selection would not have been saved. 

但我想这样:

Record has been saved successfully 
Note: If head already exists for selected department 
than your head selected would not have been saved.

function ShowMsg() {
        var MessageStatus = $("#HiddenFieldSetMessage").val();
        switch (MessageStatus) {
            case 'SavedButHeadMightExist':
                $("#Msg").text("Record has been saved successfully Note: If head already exists for the concerned department than your head selection would not have been saved");
                $("#ModalHeader").text("Success");
                break;
            case 'NotSaved':
                $("#Msg").text("Record not inserted");
                $("#ModalHeader").text("Error");
                break;
    }
    </script>

3 个答案:

答案 0 :(得分:3)

您可以使用换行符,但这些内容并未真正显示,您真正想要的可能只是中断标记

$("#Msg").html("Record has been saved successfully<br />" +
               "Note: If head already exists for the concerned department<br />" +
               "than your head selection would not have been saved");

请注意,您必须将方法从text()更改为html()

答案 1 :(得分:1)

您需要使用<br/>进行换行。并使用html代替text

text:仅用于文本,不允许使用html html:它还允许html标签

你可以在这里找到这两者之间的区别

  1. What is the difference between jQuery: text() and html() ?

答案 2 :(得分:0)

将.text更改为.html

$('#Msg').html('line1<br>line2');

OR

var escaped = $('<div>').text(stringDisplay).text();
$('#Msg').html(escaped.replace(/\n/g, '<br />'));