javascript - JavaScript - updating
问题描述
解决方案
听起来您只需要用以下内容替换++
和换行符<br>
:
var textarea = document.getElementById('textarea');
var textareaPreview = document.querySelector('.textarea-preview');
var currentText;
textarea.onkeyup = function(){
currentText = textarea.value;
var previewAsString = "<p class='p-preview'>" + currentText.replace(/\+\+|\n/g, '<br>') + "</p>"
textareaPreview.innerHTML = previewAsString;
};
textarea {
width: 300px;
height: 80px;
resize: vertical;
}
<div class="block">
<textarea id="textarea" placeholder="Type here.."></textarea>
</div>
<div class="block">
<div class="textarea-preview"></div>
</div>
推荐阅读
- javascript - Lodash过滤器功能不适用于正则表达式
- django - 在不考虑模型的情况下生成 django 站点地图?
- azure - Vbscript - OpenTextFile 在 Azure 中不起作用
- java - JSP - 不能从不同的 JSP 页面调用 java 变量?
- git - 如何避免“git gc”生成垃圾松散对象?
- html - 如何在 html5 datepicker 中禁用日期数组列表
- c# - 映射表达式
- java - 预期总是 0 ,当使用 @Mock
- javascript - 检查数组是否“包含”Javascript中的项目
- javascript - 当我在输入文本中输入 @ 时,它应该在反应中以不同的颜色显示