簡體   English   中英

HTML:如何在 textarea 中保留格式?

[英]HTML : How to retain formatting in textarea?

  • 我正在使用 HTML textarea 供用戶輸入一些數據並將其保存到 App Engine 的模型中
  • 問題是,當我檢索內容時,它只是文本,所有格式都消失了
  • 原因是因為在 textarea 中沒有我們可以制作的格式

問題:

  • 有沒有辦法保留用戶提供的格式?
  • 是否有任何其他元素(除了 textarea),我必須使用?(哪個?)

PS 我對 Web 開發領域很陌生,正在從事我的第一個項目

謝謝

您想要的是富文本編輯器 標准 HTML <textarea>標簽只接受純文本(即使文本是或包含 HTML 標記)。 那里有很多示例(包括鏈接頁面上列出的一些示例),但我強烈建議為此使用預先打包的示例。 對於新手,甚至對於很多有經驗的人來說,編寫自己的代碼是相當復雜的。 TinyMCECKEditor都是非常常見的,但還有很多其他的。

文本框就像寫字板,你不能格式化它,如果你從 word 或任何其他格式化文本粘貼,它會清除所有格式,你將只剩下文本。

您需要向文本區域添加一個編輯器,我使用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>

注意:您需要下載並在目錄中包含<script language="javascript" type="text/javascript" src="/js/tiny_mce/tiny_mce.js"></script>的 js 文件

希望這可以幫助!

如果您希望某人能夠格式化他們的文本(例如使用所見即所得的粗體按鈕等),但如果您希望能夠接受預先格式化的 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>

這使用了一個contenteditable div元素,您可以將其格式化為一個輸入框並接受粘貼的 HTML,以及一個隱藏的textarea#foo ,它將在提交表單之前填充粘貼的 HTML。

請注意,這不是一個可訪問的解決方案。

根據您的程序目標,只需在 textarea 的輸入中左右添加“pre”標簽就足夠了。 例如,如果您的代碼提交到文本區域內的任何表單,然后在目標文件中回顯它,這已經可以工作了。

> File 1:
> 
>     <form action="Output.php" methode=post>
>     <textarea name="Input"></textarea>
>     </form>
> 
> File Output.php
> 
>     $UserInput = $_POST["Input"];
>     $UserInput = "<pre>" . $UserInput . "</pre>"
>     echo $UserInput

這已經將保留所有輸入,例如在原始用戶輸入中使用的地方並正確回應它們

如果您從 App Engine 中檢索內容,並在大多數情況下使用已添加的 pre 標簽保存內容就行了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM