【问题标题】:Add extra new line in the output using CSS, Javascript使用 CSS、Javascript 在输出中添加额外的新行
【发布时间】:2020-07-22 19:27:01
【问题描述】:

我正在创建一个博客网站。我正在使用 textarea 获取输入并将其存储在 MongoDB 中。用于显示我使用的数据:white-space: pre-wrap;

如果我将输入输入为:

hello
world

输出是:

hello
world

我希望输出为:

hello

world

代码: 接受输入:

<form action="/post/compose" method="post">
    <div class="form-group">
      <label for="postTitle">Title</label>
      <input type="text" class="form-control" name="postTitle" >
      <label for="postBody">Body</label>
      <textarea class="form-control" name="postBody" rows="10" col="10"></textarea>
    </div>
    <button class="btn btn-success" type="submit">Publish</button>
</form>

将输入存储在数据库中:

 const post=new Post({
      Title:req.body.postTitle,
      Content:req.body.postBody,
    
      });
      post.save();

显示输入

<div id="fullpost">
<h2 class="text-dark"><%= post.Title %></h2>
<p  class="text-dark" id="Postcontent"><%- post.Content %></p>
</div>

css:

#Postcontent{
  white-space: pre-wrap;
}

现在我添加了 white-space 属性,如果输入中有 \n ,则在输出中包含单个空格。但我想要两个空格。 是否可以在没有 Javascript 中的正则表达式的情况下做到这一点?请帮忙。提前致谢。

【问题讨论】:

  • 您可以查看此链接:stackoverflow.com/questions/49660349/…
  • 那么我怎样才能将 \n 添加到用户输入中?
  • 一点上下文会有所帮助。你应该发布你的代码
  • @FlorentArlandis 嘿!感谢您的回复。我添加了一些代码。
  • 您希望在每个条目后有一个新的空白行,还是希望文本在显示的每行文本之间显示 100% 的行高?

标签: javascript html css node.js express


【解决方案1】:

好吧,听起来您想要显示在原始文本中输入的换行符?

在这种情况下,您可能不需要&lt;textarea&gt;。您可能想使用this approach,通过将textarea 替换为contenteditable=true 的元素(例如div),然后使用JavaScript 函数捕获div 的内容(您将无法通过标准表单提交获得它)。

【讨论】:

  • ?我们有更简单的解决方案吗?
  • 如果您想使用&lt;textarea&gt;,则不要。问题是当表单有效负载到达您的服务器时,您将无法区分空格和换行符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-17
  • 2017-06-29
相关资源
最近更新 更多