简体   繁体   English

如何在JavaScript中编写简单的WYSIWYG编辑器?

[英]How does one go about writing a simple WYSIWYG editor in JavaScript?

In my project I need to write small WYSIWYG editor (just let users make text bold/italic/underline and maybe some more). 在我的项目中,我需要编写一个小的WYSIWYG编辑器(只是让用户使文本粗体/斜体/下划线,也许还有更多)。 So I wouldn't like to use monsters like tinymce or fckeditor. 所以我不想使用像tinymce或fckeditor这样的怪物。 My question is what HTML elements and javascript functions do I need to know for it? 我的问题是我需要知道的HTML元素和JavaScript函数是什么? An HTML element in which I can edit and format text is a very interesting thing in this question. 在这个问题中,我可以编辑和格式化文本的HTML元素是一件非常有趣的事情。

If you want to roll your own, look at Midas . 如果你想自己动手,请看看迈达斯 It's available in FF3+, IE, Safari, Opera, Chrome. 它可用于FF3 +,IE,Safari,Opera,Chrome。

Basically, you use contentEditable and execCommand to turn the browser into an RTE. 基本上,您使用contentEditable和execCommand将浏览器变为RTE。

document.getElementById('edit').contentDocument.designMode = "on";

请访问http://www.mozilla.org/editor/ie2midas.html

There is http://www.gosu.pl/steditor/ which code is quite simple and clear. http://www.gosu.pl/steditor/有哪些代码非常简单明了。 It uses designMode and execCommand as was mentioned in other answers. 它使用了其他答案中提到的designMode和execCommand。

Alternatively use simple languages like markdown or textile and provide a live preview. 或者使用简单的语言,如markdown或textile,并提供实时预览。

I think most people will get asterisks for emphasis and double ones for bolding . 我想大多数人会得到强调的星号加粗的双倍 星号

我过去使用过YUI的所见即所得编辑器,它允许你配置按钮,这样你就可以轻松使用它并将按钮限制为粗体/斜体/下划线。

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

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