簡體   English   中英

是否可以在不改變樣式的情況下將 html 轉換為純文本?

[英]Is it possible to convert html to plain text without changing style?

有沒有辦法在不改變格式/樣式的情況下將富文本編輯器值傳遞給普通文本中的 controller? 我正在使用 cleditor 作為文本編輯器。 這是我的 textarea 元素以及我如何調用它

$(document).ready(function() {
  var options = {
    width: 600,
    height: 300,
    controls: "bold italic underline strikethrough subscript superscript | font size style | color highlight removeformat | bullets numbering | outdent indent | alignleft center alignright justify | undo redo | rule link image unlink | cut copy paste pastetext | print source"
  };
  var editor = $("#editor").cleditor(options)[0];
})
<link href="~/CLEditor1_4_5/jquery.cleditor.css" rel="stylesheet" />
<script src="~/bootstrap-3.3.6-dist/js/jquery.min.js"></script>
<script src="~/CLEditor1_4_5/jquery.cleditor.js"></script>


<textarea rows="0" id="editor" cols="0" style="margin-left:0%" placeholder="Write message here"></textarea>

我想將文本區域的值發送到 controller。 我正在使用 val() 來執行此操作。 But when any styling is selected ie bold or italic, it's passing HTML value like this.

 <span style="font-weight: bold; font-style: italic; text-decoration-line: underline;">hello</span>

我想將它作為純文本傳遞,但不更改 styles。我已經完成了轉換代碼,但他們給了我純文本。 那么是否可以在不更改 styles 的情況下將其轉換為普通字符串? 請幫幫我

如果您談論的是標簽剝離,那么如果您不必擔心標簽之類的事情,那就相對簡單了。 如果您需要做的只是顯示沒有標簽的文本,您可以使用正則表達式來完成:

<[^>]*>

如果您確實需要擔心標簽等問題,那么您將需要比正則表達式更強大的東西,因為您需要跟蹤 state,這更像是上下文無關語法 (CFG)。 盡管您可以通過“從左到右”或非貪婪匹配來完成它。

如果您可以使用正則表達式,則有許多 web 頁面有很好的信息:

http://weblogs.asp.net/rosherove/archive/2003/05/13/6963.aspx http://www.google.com/search?hl=en&q=html+tag+stripping+&btnG=Search If you需要更復雜的 CFG 行為我建議使用第三方工具,不幸的是我不知道有什么好推薦的。

您可以在 Razor 中使用encodeURI()decodeURI() JavaScript 函數,或HttpUtility.UrlEncode()Httputlity.UrlDecode() 這些函數會將所有特殊字符編碼為一種格式,例如保存以存儲在數據庫中。 Once you want to use the data you can simply decode the string using the functions mentioned above, and load the html on a different place (for example with Html.Raw in razor, or by setting the innerHTML property of an element using JavaScript.

暫無
暫無

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

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