How do I load CKEditor with Javascript?

青春壹個敷衍的年華 提交于 2019-12-10 20:08:38

问题


I have integrated CKEditor into my commenting system for my blog, and it shows up fine when I load a new page.

However, for reply comments, I have a javascript action that opens a new comment form from a "reply to this comment" link below each comment. In this case, the CKEditor doesn't load (I just get a basic non-CKEditor text-area). Do I need to add something to my javascript file so that the CKeditor loads correctly?

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:


You have to create new instance of editor. After appending form in comments/new.js.erb:

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

I'm not sure is it possible to use any selector instead of 'id_of_textarea'. If not then textarea must have unique id. There is also another way to create editor instances but I have not tried it myself



来源:https://stackoverflow.com/questions/14382264/how-do-i-load-ckeditor-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!