【问题标题】:How to fix message to appear in new line?如何修复消息出现在新行中?
【发布时间】:2019-04-25 08:43:33
【问题描述】:

我创建了一个用 javascript 编写的聊天机器人程序。每当用户按 Enter 键时,响应就会出现在同一行而不是换行。正如建议的那样,我也添加了 loadresponse() 的代码。这是代码的sn-p:

// function to display success response in conversation div
function loadResponse(lexResponse) {

    var conversationDiv = document.getElementById('conversation');
    var responsePara = document.createElement("P");
    responsePara.className = 'lexResponse';
    if (lexResponse.message) {
        responsePara.appendChild(document.createTextNode(lexResponse.message));
        responsePara.appendChild(document.createElement('br'));
    }

消息没有出现在新行中,而是出现在同一行中

【问题讨论】:

  • 显示响应的部分是什么?如果是loadResponse() 函数,请同时显示该代码。
  • 我已经编辑了代码
  • 添加 conversationDiv.appendChild(responsePara); 并确保您的 css 中没有 .lexResponse { display: inline; }
  • 此行是否也称为新行:(responsePara.appendChild(document.createElement('br')); ?
  • 是的,检查工作示例:jsfiddle.net/herbowicz/u4Ldjg2f/4

标签: javascript jquery newline chatbot


【解决方案1】:

您可以将<br /> 附加到innerHTML 属性。

responsePara.innerHTML = `${responsePara.innerHTML}<br />`;

【讨论】:

  • 响应仍在同一行
  • 你想要响应前的
    吗? &lt;br /&gt;${responsePara.innerHTML}&lt;br /&gt;;
  • 我仍然遇到同样的问题
猜你喜欢
  • 2020-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
  • 2010-10-21
  • 2018-01-14
  • 1970-01-01
相关资源
最近更新 更多