javascript 将文本添加到 Froala 文本编辑器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24893209/
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-10-28 03:31:18  来源:igfitidea点击:

Add text to Froala text editor

javascriptjqueryfroala

提问by Alen

I'm trying to programatically input html into Froala text editor. Based on their documentation this should work:

我正在尝试以编程方式将 html 输入到 Froala 文本编辑器中。根据他们的文档,这应该有效:

$(function(){
    $('#editor').editable("setHTML", "<p>My custom paragraph.</p>", false);
});

But when I reload page I only get empty space, no errors. This is binded to textarea with id #editor. If I use this code:

但是当我重新加载页面时,我只会得到空白空间,没有错误。这与 id 绑定到 textarea #editor。如果我使用此代码:

$(function(){
    $('#editor').editable({inlineMode : false});
});

... then everything is ok.

......然后一切正常。

Does anyone knows how to programatically input html into this editor.

有谁知道如何以编程方式将 html 输入到这个编辑器中。

回答by muhammad assegaf

You should try this :

你应该试试这个:

$('#editor').froalaEditor('html.set', 'My custom paragraph.');

See the detail about froalamethod at this reference: https://www.froala.com/wysiwyg-editor/docs/methods#html.set

froala在此参考中查看有关方法的详细信息:https: //www.froala.com/wysiwyg-editor/docs/methods#html.set

回答by Hans Roerdinkholder

Use both. First you have to create the editor, then you can set the HTML

两者都用。首先你必须创建编辑器,然后你可以设置 HTML

$(function(){
    $('#editor').editable({inlineMode : false});
    $('#editor').editable("setHTML", "<p>My custom paragraph.</p>", false);
});

回答by Ilyas karim

For the users you are looking for v3 answers, You can add text in v3 by:

对于您正在寻找 v3 答案的用户,您可以通过以下方式在 v3 中添加文本:

var editor = new FroalaEditor('.selector', {}, function () {
  // Call the method inside the initialized event.
  editor.html.insert('<p>My custom paragraph.</p>');
})

Also for if you want to replace the whole content then you can use set method

另外如果你想替换整个内容,那么你可以使用 set 方法

var editor = new FroalaEditor('.selector', {}, function () {
  // Call the method inside the initialized event.
  editor.html.set('<p>My custom paragraph.</p>');
})

回答by Mendon Ashwini

Try this:

试试这个:

$('#editor').froalaEditor('html.set', 'My custom paragraph.');
$('#editor').froalaEditor('undo.saveStep', 'My custom paragraph.');

Reference: https://github.com/froala/wysiwyg-editor/issues/1578

参考:https: //github.com/froala/wysiwyg-editor/issues/1578

回答by RailsEnthusiast

After inserting the HTML use "Sync"

插入 HTML 后使用“同步”

$("#editor").editable("insertHTML", "123456", true);
$("#editor").editable("sync");

回答by Linh ?ào Ng?c

Just for note. in HTML

仅供参考。在 HTML 中

<div id="froala-editor">
  <p>something</p>
</div>

and in javascript

在 javascript 中

  $(function() {
    $('div#froala-editor').froalaEditor({
      pastePlain: true
    })
  });