如何使用JavaScript加载CKEditor?

3
我已将CKEditor集成到我的博客评论系统中,当我加载新页面时,它能正常显示。
但是,对于回复评论,我有一个JavaScript操作,可以从每个评论下面的“回复此评论”链接打开一个新的评论表单。在这种情况下,CKEditor不会加载(我只得到一个基本的非CKEditor文本区域)。我需要在我的JavaScript文件中添加一些内容才能使CKEditor正确加载吗? posts/show.html.erb
<%= @post.content %>
<%= render 'comments/form' %> #this CKEditor form renders fine when the page loads
<%= @post.comments %>

comments/_form.html.erb

<%= simple_form_for(@comment, remote: true) do |f| %>
  <%= f.hidden_field :parent_id %>
  <%= f.cktext_area :content, :input_html => { :ckeditor => { :toolbar => 'Basic' } }, :class => "comment_input" %>
<%= f.button :submit %>

comments/new.js.erb

$('#comment_<%= @comment.parent.id %>').append("<%= escape_javascript(render 'form') %>");

comments/_comment.html.erb

<div id="comment_<%= comment.id %>">
  <%= comment.content.try(:html_safe) %>
  <%= link_to "reply to this comment", new_comment_path(:parent_id => comment), remote: true %>
</div>
1个回答

1

您需要创建一个新的编辑器实例。在将表单附加到comments / new.js.erb中后:

CKEDITOR.replace('id_of_textarea',{
    :toolbar => 'Basic'
});

我不确定是否可以使用任何选择器来代替'id_of_textarea'。如果不行,文本区域必须有唯一的id。 还有另一种方法创建编辑器实例,但我自己没有尝试过。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接