javascript 如何在向 div 标签添加文本时添加换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12266809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:45:13  来源:igfitidea点击:

How to add line breaks while adding text to div tag

javascriptjqueryhtml

提问by Vani

All, I am working on populating a div tag with text.

所有,我正在用文本填充 div 标签。

var centName;
 centName=centName+$(this).attr('data-centerName');//tried <br/> / '\n'

 $("#selOccs").text(centName.Trim(';'));

My div tag is like

我的 div 标签就像

  <div ID="selOccs" NAME="selOccs"  style="width: 350px; height: 100px;border:1px solid ;
                   overflow-y: scroll; scrollbar-arrow-color: blue; scrollbar-face-color: #e7e7e7; scrollbar-3dlight-color: #a0a0a0; scrollbar-darkshadow-color: #888888">

   </div>

The issue is adding line breaks. I need display the centName with line breaks.so far all the values are coming in one line[ as per the code].

问题是添加换行符。我需要用换行符显示 centName。到目前为止,所有值都在一行中[根据代码]。

I tried adding <--br/--> and '\n' but itseems to be not working..

我尝试添加 <--br/--> 和 '\n' 但它似乎不起作用..

回答by VVV

Use .html()instead of text and in the string you can then use <br/>

使用.html()而不是文本,然后在字符串中使用<br/>

回答by Anthony Simmon

var centName;

centName = centName + $(this).attr('data-centerName') + '<br/>';

$("#selOccs").html(centName.Trim(';'));