gpt4 book ai didi

javascript - 无法插入换行符\n

转载 作者:行者123 更新时间:2023-11-30 14:16:03 25 4
gpt4 key购买 nike

在下面的简化示例中,字符串“quis”被替换为用户在输入框中输入的内容。我也希望能够从输入框中插入换行符。

所以如果用户输入类似"blabla\nbla"结果应该是

“啪啪啪”

小白"

我不想使用像
这样的 HTML 标签

function replace_with(e) {
var replacement = document.getElementById('replaceWith').value;
var text = document.getElementById('val');
var reg = new RegExp("quis", "g");
text.innerHTML = globalString.replace(reg,replacement);
}

document.getElementById('replaceWith').addEventListener('keyup', replace_with);

var globalString = "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.";
#val{
border: 1px solid grey;
padding: 5px;
}
.input{
border: 1px solid grey;
padding: 5px;
margin-bottom: 10px;
}
<input type="text" spellcheck="false" class="input" id="replaceWith">

<div id="val" contenteditable="true" spellcheck="false">
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.
</div>

最佳答案

<br>在 innerHTML 而不是 \n获取换行符

关于javascript - 无法插入换行符\n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53559422/

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