简体   繁体   English

jQuery / JavaScript中的换行符

[英]Line break in jquery/ javascript

nowadays i am upto java script, jquery etc. i am facing little problem. 如今,我正在使用Java脚本,jquery等。我面临的问题很少。

i am writing a text message inside CASE block , it's a long string when displayed: 我在CASE块内写了一条文本消息,显示时是一个长字符串:

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

but i want it like this: 但我想要这样:

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>

You could use newlines, but those aren't really shown, what you really want is probably just break tags 您可以使用换行符,但是并没有真正显示这些换行符,您真正想要的只是断开标签

$("#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");

Note that you have to change the method from text() to html() 请注意,您必须将方法从text()更改为html()

you need to use <br/> for line break. 您需要使用<br/>进行换行。 and use html instead of text 并使用html而不是text

text: it is used for text only no html is allowed 文字:仅用于文字,不允许html
html: it allow html tags also html:它也允许html标签

You can find difference between these two here 您可以在这里找到这两者之间的区别

  1. What is the difference between jQuery: text() and html() ? jQuery:text()和html()有什么区别?

Change .text to .html 将.text更改为.html

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

OR 要么

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

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

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