将纯文本粘贴到可编辑的div中 [英] Paste only plain-text into editable div

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

问题描述

我需要能够让用户粘贴到可编辑的div(通过用户选择的任何东西:右键单击并粘贴,快捷键等),但是我想放弃格式化,只能使用纯文本。

i need to be able to allow a user to paste into an editable div (via whatever the user chooses: right-click and paste, shortcut key, etc), but i want to discard formatting and only take the plain text.

我无法使用textarea,因为如果由用户启动的事件应用,div将允许基本格式(粗体和斜体)。

i can't use a textarea since the div will allow basic formatting (bold and italic) if applied by user-initiated events.

onbeforepaste事件看起来很有前途,但根据quirksmode的支持是如此有限,以至于无法使用。

the onbeforepaste event looked promising, but according to quirksmode the support is so limited as to be unusable.

tyia任何建议

推荐答案

也可以使用javaScript进行黑客攻击。希望这有帮助

It can also be hacked by using javaScript. Hope this helps

我创建了一个示例这里

<div contentEditable="true" id="clean-text-div"> </div>
<div id="clean-btn"> Clean Me</div>
<div id="message"> paste html content and clean background HTML for pure text</div>


$("#clean-btn").click(function(){
    $("#clean-text-div").text($("#clean-text-div").text());
    $("#message").text("Your text is now clean");
});

这篇关于将纯文本粘贴到可编辑的div中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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