我想将文本粘贴到:
<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/41721331/