JavaScript将文本粘贴到具有可删除格式的contenteditable [英] JavaScript paste text to contenteditable with remove format

查看:133
本文介绍了JavaScript将文本粘贴到具有可删除格式的contenteditable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文本粘贴到:

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

然后粘贴后,我需要该文本将具有已删除的文本格式,但要保留新行.

Then after paste i need that text will be with removed text formatting, but keep new lines.

我有这样的文字:

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

但这不是添加新行;

推荐答案

我发现了我需要的东西.这段代码完成了我所需的工作:

I've found out what I needed. This code does my required job:

$(".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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆