简体   繁体   English

textarea中的粗体文字

[英]bold text in textarea

i'm trying to write a very simple wysiwyg editor, where users can simply add bold text, hyperlinks and bullets, after selecting part of the text.. actualy just like CKEditor. 我正在尝试编写一个非常简单的所见即所得的编辑器,用户可以在选择部分文本后简单地添加粗体文本,超链接和项目符号。实际上就像CKEditor一样。

But as far as i know there is no way to add formatting to a textarea. 但据我所知,没有办法为textarea添加格式。 So I would like to know, how do other wysiwyg editors like CKEditor solve this. 所以我想知道,像CKEditor这样的其他所见即所得的编辑如何解决这个问题。

Well, this doesn't answer the question about how they do it, but you can apply styling to a textarea. 好吧,这并没有回答关于他们如何做的问题,但你可以将样式应用到textarea。 Just try: 你试一试:

<textarea style="font-weight:bold;"> </textarea>

As to how editors like the CKEditor do it, most do it by applying a ton of javascript and css to make a <div> seem like a <textarea> . 至于像CKEditor这样的编辑如何做到这一点,大多数是通过应用大量的javascript和css来使<div>看起来像<textarea> That's how I did it in a MS class on AJAX. 这就是我在AJAX的MS课上做到的。 Also, if you view the rendered source on the CKEditor demo , you'll see that everything for the input area is a combination of <div> elements. 此外,如果您在CKEditor演示中查看渲染源,您将看到输入区域的所有内容都是<div>元素的组合。

The browser based WYSIWYG editors work by building the HTML for the document/text you're editing. 基于浏览器的WYSIWYG编辑器通过为您正在编辑的文档/文本构建HTML来工作。 In other words, you're essentially editing HTML inside the browser and not the text in a TEXTAREA . 换句话说,您实际上是在浏览器中编辑HTML而不是TEXTAREA的文本。 You might want to have a look at the source code for Rich Text Editor and check out Mozilla's Midas Specification before you embark on writing everything from scratch. 在开始从头开始编写所有内容之前,您可能需要查看Rich Text Editor的源代码并查看Mozilla的Midas规范

You need to use the contenteditable attribute. 您需要使用contenteditable属性。 A google search will give you a lot of information and examples 谷歌搜索将为您提供大量信息和示例

Most WYSIWYG, like TinyMCE use pure JavaScript overlays in order to give a rich text editor which looks for textarea html tags to replace a basic text editor with their rich one. 大多数所见即所得,就像TinyMCE一样,使用纯JavaScript覆盖,以便提供一个富文本编辑器,该编辑器查找textarea html标签,用其丰富的文本编辑器替换基本文本编辑器。

What the javascript editor is really doing is adding the appropriate html tags like <b> or <i> to the text contents. javascript编辑器真正做的是在文本内容中添加适当的html标签,如<b>或<i>。

That's why when you submit the contents of a WYSIWYG editor to the server you typically get html. 这就是为什么当你将WYSIWYG编辑器的内容提交给服务器时,你通常会得到html。

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

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