[英]How to get the plain text from summernote editor?
例如,這是我輸入的內容:
sdf
42342
xxcv
.code()
將其轉換為sdf<br>42342<br>xxcv
或另一件事:
[{"_type":"ServerOperation","operationType":"ANNOUNCE"}]
成為
<span class="message_content">[{"_type":"ServerOperation","operationType":"ANNOUNCE"}]</span>
如何獲得純/純文本?
嘗試一下:
var plainText = $($("#summernote").code()).text()
編輯 :在較新的版本中,您需要使用此代替:
var plainText = $($("#summernote").summernote("code")).text()
您可以對JavaScript問題應用前兩個答案之一:如何從字符串中剝離HTML標簽? ,在.code()
之后刪除標簽。
ReactiveRaven的答案(使其保持一行)對我來說很好:
cleanText = $("#summernote").code().replace(/<\/?[^>]+(>|$)/g, "");
例如,使用[{"_type":"ServerOperation","operationType":"ANNOUNCE"}]
:
$("#summernote").code()
返回
<p>[{"_type":"ServerOperation","operationType":"ANNOUNCE"}]<br></p>
$("#summernote").code().replace(/<\\/?[^>]+(>|$)/g, "")
返回
[{"_type":"ServerOperation","operationType":"ANNOUNCE"}]
沒有任何標簽。
如果要保留回車符,可以在應用鏈接的問題上指定的解決方案之前,將</p>
和<br>
替換為\\n
:
$("#summernote").code()
.replace(/<\/p>/gi, "\n")
.replace(/<br\/?>/gi, "\n")
.replace(/<\/?[^>]+(>|$)/g, "");
你可以用這個
var code = $("#editor").code();
var text = code.replace(/<p>/gi, " ");
var plainText= $("<div />").html(text).text();
較新的版本
var contents = $('#summernote').summernote('code');
var plainText = $("<p>" + contents+ "</p>").text();
添加虛擬標簽
解決了缺乏格式的問題。
我需要檢查textarea是否有任何內容。 這樣就可以了:
當前的summernote版本(8):
$($("#summernote").summernote('code').replace(/ |<br>/g, ' ')).text().trim() == ''
在我的舊版本中:
$($("#summernote").code().replace(/ |<br>/g, ' ')).text().trim() == ''
嘗試這個 :)
$('#summernote').summernote ('code', '<b> hello world </ b>');
對於 0.8.20 版:
如果你想要實際的文本,沒有 Summernote 的任何樣式(粗體、斜體等)並且沒有任何換行符:
$("<div />").html($("#summernote").summernote("code")).text();
如果你想要文本,Summernote 的樣式和換行符:
$("<textarea />").html($("#summernote").summernote("code")).text();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.