gpt4 book ai didi

MySQL 未在 HTML 中添加新行

转载 作者:行者123 更新时间:2023-11-29 15:23:08 25 4
gpt4 key购买 nike

使用 NodeJS 和 ExpressJS,我从 MySQl 传递数据,它有新行,但当我在 HTML 页面上显示它时没有,这里证明它显示新行(随机文本)

the text

results from database

the html page

这是在 html 中渲染时的代码

<span><%=userRoles[0].description%></span>

我需要它在行中添加新行

最佳答案

方法一:将所有换行符( \r\n\r\n )替换为 <br>标签

您需要使用<%- code %>而不是<%= code %>以避免 html 转义。

<span><%- userRoles[0].description.replace(/(?:\r\n|\r|\n)/g, '<br>'); %></span>

方法2:使用<pre>标签

<pre>元素将显示换行符

<pre><%- userRoles[0].description %></pre>

关于MySQL 未在 HTML 中添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59232076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com