在javascript span中显示的文本中插入换行符
insert newline in text displayed in span from javascript
我有一个像这样的span:
<span class "message"></span>
我用javascript填充它,像这样:
message = "first message" + "'''n";
message = message + "second message";
$(".message".)text(errorMessage);
$(".message").show();
问题是,当文本显示在我的浏览器上时,2条消息在同一行,我不能用换行符插入第二行。而在我的调试控制台中,文本显示在2行上。我还尝试了<br>
,这是最糟糕的,因为它没有被解释,所以我得到这样的消息:
first message br second message.
基本上,我想显示:
first message
second message
您可以使用<br />
和html()
在新行上分隔文本:
message = "first message" + "<br />";
message += "second message";
$(".message").html(errorMessage).show();
设置匹配元素集合中每个元素的HTML内容。
文档:http://api.jquery.com/html/
首先,你的HTML中有一个错误,它应该是:
<span class="message"></span>
JS中还有一个错误:
$(".message".)text(errorMessage); // <-- Incorrect
$(".message").text(errorMessage); // <-- Correct
但我相信这只是OP中提到的一个错别字
的问题,是当文本显示在我的浏览器上,…
解决方案:不需要任何花哨的JS或更改代码。实现您想要的最简单的解决方案是在span
上使用CSS属性(mozilla文档,w3规范)white-space
。它有很好的浏览器支持。
white-space: pre-wrap; /* Allows wrapping */
/* Other possibilities for similar(not same) */
/* white-space: pre-line; */
/* white-space: pre; */
检查demo
的代码片段.message.prewrap {
white-space: pre-wrap;
}
.message.preline {
white-space: pre-line;
}
.message.pre {
white-space: pre;
}
/* Not required, only for demo */
/* ================ */
.box {
border: 1px solid #d0d5de;
}
.title {
background: #d0d5de;
}
.title > span {
font-weight: bold;
text-decoration: underline;
}
/* ================ */
<div class="title">
<pre>white-space: pre-wrap;</pre>
<span>Will retain white-spaces like new-lines, tabs and spaces. It will wrap the text</span>
</div>
<div class="box">
<span class="message prewrap">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</span>
</div>
<hr>
<div class="title">
<pre>white-space: pre-line;</pre>
<span>Will retain new-lines but lose tabs and spaces. It will wrap the text</span>
</div>
<div class="box">
<span class="message preline">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</span>
</div>
<hr>
<div class="title">
<pre>white-space: pre;</pre>
<span>will retain only new-lines and will not wrap the text.</span>
</div>
<div class="box">
<span class="message pre">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</span>
</div>
我意识到这是一个老问题,但如果其他人正在寻找进一步的解决方案,在CSS中使用display: table
的span
元素的选项对我来说效果很好。
这篇css-tricks文章有一些有用的想法:https://css-tricks.com/injecting-line-break/
相关文章:
- 将文本插入光标所在的文本区域
- 从 AngularJS 将文本插入 HTML
- 如何在 javascript 中使用 .html() 将 html 作为文本插入
- 如何将文本插入到几个带有 id 的文本区域中的一个文本区域中
- 将文本插入<text区域>
- 如何将翻译后的文本插入数据库 php
- 在 JavaScript 中,将文本插入字符串的最简单方法是什么?
- 将文本插入到其他页面上的文本区域中
- 如何将文本插入到由 raphaeljs 创建的节点中
- 纯Javascript将文本插入中,不带属性
- Chrome 扩展程序:点击弹出窗口中的按钮时,将文本插入文本字段
- 通过查询将文本插入文本区域
- 单击链接并将隐藏字段中的文本插入输入字段超过 1 次,然后替换输入字段中的文本
- 使用 jQuery/Javascript 将鼠标悬停在按钮上时将文本插入文本和帮助文本
- 需要解决方案在 NicEdit 将 HTML 文本插入实例
- 如何将HTML文本插入jQuery
- 使用javascript为谷歌chrome扩展插件将文本插入文本区域
- 按钮点击文本插入
- 防止移动默认键盘同时保留文本插入光标
- jQuery:将格式化的文本插入文本区域