简体   繁体   English

如何在HTML中实现富文本编辑器?

[英]How to implement a Rich Text Editor in HTML?

Here is the demo: 这是演示:

http://www.kevinroth.com/rte/demo.htm http://www.kevinroth.com/rte/demo.htm

when I use firebugs to inspect the source code, I see that it is iFrame only, but how can the iFrame have a text area behavior?? 当我使用firebugs来检查源代码时,我发现它只是iFrame,但iFrame如何才能有文本区域行为? Any ideas on how to implement this? 有关如何实现这一点的任何想法? Thank in 谢谢

As I'm doing this for my job right now, I've done a small amount of of research. 我正在为我的工作做这件事,我做了一些研究。 From what I have figured out, there are two ways to accomplish this: 根据我的想法,有两种方法可以实现这一目标:

document.designMode document.designMode

Using document.designMode in JavaScript which sets the whole HTML document to be editable. 在JavaScript中使用document.designMode ,它将整个HTML文档设置为可编辑。 As the whole HTML document is editable, presumably an iframe is needed to encapsulate the editing, so that the user can't edit any part of the page you don't want editing. 由于整个HTML文档是可编辑的,因此可能需要使用iframe来封装编辑,以便用户无法编辑您不想编辑的页面的任何部分。

From what I can tell, the demo you linked and TinyMCE uses this method. 据我所知,您链接的演示和TinyMCE使用此方法。

contenteditable CONTENTEDITABLE

The contenteditable HTML attribute is similar but doesn't require an iframe to be used. contenteditable HTML属性类似,但不需要使用iframe You add the attribute to a tag and all of the HTML within it becomes editable with a blinking cursor. 您可以将该属性添加到标记中,并使用闪烁的光标将其中的所有HTML变为可编辑。

Here is a demo of it: http://html5demos.com/contenteditable 以下是它的演示: http//html5demos.com/contenteditable


Notes 笔记

In that frame you can see a full HTML source that builds up something that looks like a text area + toolbar + stuff. 在该框架中,您可以看到一个完整的HTML源代码,它可以构建一个看起来像文本区域+工具栏+东西的东西。 A complicated Javascript is doing all the trick behind. 一个复杂的Javascript正在做所有的技巧。 Eg if you select some text and make it bold with the toolbar than the script places <strong> tags around your text and so on.. 例如,如果您选择一些文本并使用工具栏将其加粗,那么脚本会在文本周围放置<strong>标记等等。

Use http://www.tinymce.com/ It is the best rich editor out there. 使用http://www.tinymce.com/这是最好的编辑器。 Works great with any browser and it has many many options. 适用于任何浏览器,它有很多选项。 It's very easy to use. 它非常易于使用。 Just look at the examples and docs. 只需看看示例和文档。 It's included with many popular blogs and CMS. 它包含在许多流行的博客和CMS中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM