gpt4 book ai didi

JavaScript 将文本粘贴到带有删除格式的 contenteditable

转载 作者:行者123 更新时间:2023-11-27 23:34:39 25 4
gpt4 key购买 nike

我想将文本粘贴到:

<div class="text" contenteditable="true"></div>

然后在粘贴后我需要文本将删除文本格式,但保留新行。

我有这段文字:

$(".text").bind({
paste: function () {
setTimeout(function () {
var text = $(".text").text();
$('.text').text(text);
}, 100);
}
});

但它不是添加新行;

最佳答案

我找到了我需要的东西。此代码完成我需要的工作:

$(".text").bind({
paste: function () {
setTimeout(function () {
var text = $(".text").html();

text = text.replace(/<p[^>]*>/g, '').replace(/<\/p>/g, '<br><br>');
text = text.replace(/<div[^>]*>/g, '').replace(/<\/p>/g, '<br><br>');

$('.text').html(text);

$(".text *").not("br").each(function() {
var content = $(this).contents();
$(this).replaceWith(content);
});

}, 1);
}
});

关于JavaScript 将文本粘贴到带有删除格式的 contenteditable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57307047/

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