jQuery 如何从summernote编辑器获取纯文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29537944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:36:31  来源:igfitidea点击:

How to get the plain text from summernote editor?

jqueryhtmlcsssummernote

提问by John Smith

for example, this is what I entered:

例如,这是我输入的内容:

sdf
42342
xxcv

.code()converts it to sdf<br>42342<br>xxcv

.code()将其转换为 sdf<br>42342<br>xxcv

or another thing:

或另一件事:

[{"_type":"ServerOperation","operationType":"ANNOUNCE"}]

becames

变成了

<span class="message_content">[{"_type":"ServerOperation","operationType":"ANNOUNCE"}]</span>

how to get the pure / plain text?

如何获得纯/纯文本?

回答by Dilshod Zopirov

Just try this:

试试这个:

var plainText = $($("#summernote").code()).text()

EDIT: In newer versions you need use this instead:

编辑:在较新的版本中,您需要使用它:

var plainText = $($("#summernote").summernote("code")).text()

回答by Alvaro Montoro

You could apply one of the top two answers for the question JavaScript: How to strip HTML tags from string?, after the .code()to strip the tags.

您可以为问题JavaScript应用前两个答案之一:如何从字符串中剥离 HTML 标签?, 在.code()剥离标签之后。

ReactiveRaven's answer (to keep it to one line) works fine for me:

ReactiveRaven 的答案(保持在一行)对我来说很好用:

cleanText = $("#summernote").code().replace(/<\/?[^>]+(>|$)/g, "");

For example, With [{"_type":"ServerOperation","operationType":"ANNOUNCE"}]:

例如,使用[{"_type":"ServerOperation","operationType":"ANNOUNCE"}]

  • $("#summernote").code()returns

    <p>[{"_type":"ServerOperation","operationType":"ANNOUNCE"}]<br></p>

  • $("#summernote").code().replace(/<\/?[^>]+(>|$)/g, "")returns

    [{"_type":"ServerOperation","operationType":"ANNOUNCE"}]

    without any tags.

  • $("#summernote").code()返回

    <p>[{"_type":"ServerOperation","operationType":"ANNOUNCE"}]<br></p>

  • $("#summernote").code().replace(/<\/?[^>]+(>|$)/g, "")返回

    [{"_type":"ServerOperation","operationType":"ANNOUNCE"}]

    没有任何标签。



And if you want to preserve the carriage return, you could replace </p>and <br>for a \nbefore applying the solution specified on the linked question:

如果你想保留回车,你可以更换</p>,并<br>\n应用的链接的问题指定的解决方案之前:

$("#summernote").code()
                .replace(/<\/p>/gi, "\n")
                .replace(/<br\/?>/gi, "\n")
                .replace(/<\/?[^>]+(>|$)/g, "");

回答by Ali Panahian

you can use this

你可以用这个

   var code = $("#editor").code();
var text = code.replace(/<p>/gi, " ");
var plainText= $("<div />").html(text).text();

回答by Toms Tony

newer versions

较新的版本

var contents = $('#summernote').summernote('code');
var plainText = $("<p>" + contents+ "</p>").text();

Adding a dummy tag

添加虚拟标签

resolves the lack of formatting.

解决缺少格式的问题。

回答by Glauco A. Amigo

I needed to check if the textarea had any content. This have done the trick:

我需要检查 textarea 是否有任何内容。这已经成功了:

Current summernote version (8):

当前 Summernote 版本 (8):

$($("#summernote").summernote('code').replace(/&nbsp;|<br>/g, ' ')).text().trim() == ''

In my older version:

在我的旧版本中:

$($("#summernote").code().replace(/&nbsp;|<br>/g, ' ')).text().trim() == ''

回答by Fazil Raza

Try This :)

尝试这个 :)

$('#summernote').summernote ('code', '<b> hello world </ b>');