简体   繁体   English

基本的javascript所见即所得编辑器

[英]Basic javascript wysiwyg editor

Can I get an explanation on how to make a wysiwyg editor using a textarea? 我可以得到有关如何使用文本区域制作所见即所得编辑器的解释吗? All I need it to be able to do is parse basic html tags like bold, italics, underline, etc. It doesn't need to have any buttons that inserts it, I just want to have a default text inside the textarea tags that parse the html. 我需要做的就是解析基本的html标签,例如粗体,斜体,下划线等。它不需要插入任何按钮,我只想在解析的textarea标签内包含默认文本html。

Example: 例:

<textarea cols="20" rows="20" name="ok">
<b>wat</b>
</textarea>

This will print out <b>wat</b> instead of wat inside the textarea. 这将打印出<b>wat</b>而不是文本区域内的wat

Edit: jQuery is preferred 编辑:jQuery是首选

A textarea cannot parse HTML -- period. textarea无法解析HTML句点。 (Anyone can feel free to correct me on this) (任何人都可以在此方面纠正我)

The WYSIWYG editors that you see are not in a textarea, at least not in the same way. 您看到的WYSIWYG编辑器不在文本区域中,至少不是以相同的方式。 I suggest using a prebuilt editor such as TinyMCE or FCK Editor. 我建议使用诸如TinyMCE或FCK Editor之类的预编译器。

Look into the contenteditable attribute. 查看contenteditable属性。 It's supported in many modern browsers. 许多现代浏览器都支持它。 Just add it to an element and edit away... 只需将其添加到元素并进行编辑即可...

document.getElementById('something').contentEditable = true;

Of course it doesn't work on textareas. 当然,它不适用于textareas。 You'd need to swap the textarea out with a div and make that editable. 您需要将textarea换成div并使其可编辑。 You'd also need to make sure the textarea has the contents (eg innerHTML ) of the div as its value when the form is submitted. 您还需要确保textarea在提交表单时将div的内容(例如, innerHTML )作为其值。

替代文字

A textarea will not parse HTML, but by using a WYSIWYG plugin, an editor will replace the textarea and give the user the ability to view and modify the content. textarea不会解析HTML,但是通过使用WYSIWYG插件,编辑器将替换textarea,并使用户能够查看和修改内容。 With some editors, such as TinyMCE, you are able to set it to Simple mode, and allow only the basics of formatting (bold, italic, underline, bullets, etc) like you are interested in. This helps keeps the editor from being cluttered with unnecessary tools. 使用某些编辑器,例如TinyMCE,您可以将其设置为“简单”模式,并且只允许您感兴趣的格式设置基础(粗体,斜体,下划线,项目符号等)。这有助于防止编辑器混乱使用不必要的工具。

I suggest checking out TinyMCE or CKEditor 我建议检查TinyMCECKEditor

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

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