基本的javascript所见即所得编辑器 [英] Basic javascript wysiwyg editor
问题描述
我可以得到有关如何使用文本区域制作所见即所得编辑器的说明吗?我需要做的就是解析基本的html标签,例如粗体,斜体,下划线等.它不需要插入任何按钮,我只想在解析的textarea标签内有一个默认文本html.
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.
示例:
<textarea cols="20" rows="20" name="ok">
<b>wat</b>
</textarea>
这将在文本区域内打印出<b>wat</b>
而不是 wat .
This will print out <b>wat</b>
instead of wat inside the textarea.
首选jQuery
推荐答案
查看contenteditable
属性.许多现代浏览器都支持它.只需将其添加到元素中并编辑即可...
Look into the contenteditable
attribute. It's supported in many modern browsers. Just add it to an element and edit away...
document.getElementById('something').contentEditable = true;
当然,它不适用于textareas.您需要将textarea换成div并使其可编辑.您还需要确保文本区域在提交表单时将div的内容(例如innerHTML
)作为其值.
Of course it doesn't work on textareas. You'd need to swap the textarea out with a div and make that editable. You'd also need to make sure the textarea has the contents (e.g. innerHTML
) of the div as its value when the form is submitted.
这篇关于基本的javascript所见即所得编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!