HTML:如何在textarea中保留格式?
问题描述:
- 我使用HTML的textarea用户输入一些数据并保存到App Engine的模型
- 的问题是,当我获取内容,这只是文字和所有格式都不见了
- 的原因是因为在文本区域没有格式化,我们可以使
- 有没有什么办法留住用户提供的格式?
- 是否有任何其他元素(除了文本域等),我将不得不使用?(哪一个?)
PS我是很新的Web开发的领域和对我的第一个项目
工作谢谢
答
你想要的是一个Rich Text Editor。标准HTML <textarea>
标签只接受纯文本(即使文本是或包含HTML标记)。这里有很多例子(包括链接在页面上列出的一些例子),但我强烈建议使用预先包装的一个例子。编码你自己对于新人来说相当复杂,甚至对于有很多经验的人也是如此。 TinyMCE和CKEditor都是很常见的,但也有很多其他的。
答
文本框就像写字板,你不能格式化它,如果你从单词或其他任何格式化文本粘贴它将擦除所有的格式,你将只剩下文本。
你需要在文本区域添加一个编辑器,我使用TinyMCE,但也有很多其他的。
为了实现你需要在你的web目录中拥有所有的源代码(你可以从TinyMCE得到)。
这里,你可以尝试一个例子:
添加这个网页的头部分:
<script language="javascript" type="text/javascript" src="/js/tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
tinyMCE.init({
theme : "advanced",
mode: "exact",
elements : "elm1",
theme_advanced_toolbar_location : "top",
theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,"
+ "justifyleft,justifycenter,justifyright,justifyfull,formatselect,"
+ "bullist,numlist,outdent,indent",
theme_advanced_buttons2 : "link,unlink,anchor,image,separator,"
+"undo,redo,cleanup,code,separator,sub,sup,charmap",
theme_advanced_buttons3 : "",
height:"350px",
width:"600px"
});
</script>
<script type="text/javascript">
tinyMCE.init({
// General options
mode : "textareas",
theme : "advanced",
plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
// Theme options
theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true,
// Skin options
skin : "o2k7",
skin_variant : "silver",
// Example content CSS (should be your site CSS)
content_css : "css/example.css",
// Drop lists for link/image/media/template dialogs
template_external_list_url : "js/template_list.js",
external_link_list_url : "js/link_list.js",
external_image_list_url : "js/image_list.js",
media_external_list_url : "js/media_list.js",
// Replace values for the template plugin
template_replace_values : {
username : "Some User",
staffid : "991234"
}
});
</script>
然后调用textarea的:
<textarea name="content" style="width:100%">YOUR TEXT HERE</textarea>
注意:您需要下载并在您的目录中的js文件<script language="javascript" type="text/javascript" src="/js/tiny_mce/tiny_mce.js"></script>
希望这有助于!
答
这不会解决你希望某人能够格式化文本的情况(例如用所见即所得的粗体按钮等),但是如果你想能够接受预格式化的HTML(例如复制和粘贴从其他HTML源,如网页),那么你可以这样做:
<form ...>
<label>Paste your HTML in the box below</label>
<textarea style='display:none' id='foo'></textarea>
<div id='htmlsource' contenteditable style='border:solid 1px black;padding:1em;width:100%;min-height:2em;' ></div>
<input type='submit' />
</form>
<script>
jQuery(function(){
jQuery('form').submit(function(e) {
jQuery('textarea').val(jQuery('#htmlsource').html());
});
});
</script>
这将使用它可以格式化看起来像一个输入框,将接受粘贴的HTML一个contenteditable
div
元素,隐藏textarea#foo
其将在提交表单之前使用粘贴的HTML填充。
请注意,这不是一个现成的解决方案。
这是最简单的解决方案。感谢contenteditable属性。拯救了我的一天! – Rasika 2014-07-13 15:51:16