- 我使用HTML的textarea用戶輸入一些數據並保存到App Engine的模型
- 的問題是,當我獲取內容,這只是文字和所有格式都不見了
- 的原因是因爲在文本區域沒有格式化,我們可以使
- 有沒有什麼辦法留住用戶提供的格式?
- 是否有任何其他元素(除了文本域等),我將不得不使用?(哪一個?)
PS我是很新的Web開發的領域和對我的第一個項目
工作謝謝
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