Fat*_*tas 2 html javascript jquery
我想将文字粘贴到:
<div class="text" contenteditable="true"></div>
Run Code Online (Sandbox Code Playgroud)
然后粘贴后,我需要该文本将具有已删除的文本格式,但要保留新行。
我有这样的文字:
$(".text").bind({
paste: function () {
setTimeout(function () {
var text = $(".text").text();
$('.text').text(text);
}, 100);
}
});
Run Code Online (Sandbox Code Playgroud)
但这不是添加新行;
我发现了我所需要的。这段代码完成了我所需的工作:
$(".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);
}
});
Run Code Online (Sandbox Code Playgroud)