jquery – CKEditor inline in modal bootstrap window

前端之家收集整理的这篇文章主要介绍了jquery – CKEditor inline in modal bootstrap window前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在模态引导程序中使用CKEditor内联,但它不起作用……

我看过这篇文章How to use CKEditor in a Bootstrap Modal?

但它与我不同,因为我正在使用内联,我只需要将CKEditor应用于某些字段(我有其他使用contenteditable属性).

JS代码

  1. CKEDITOR.disableAutoInline = true;
  2. CKEDITOR.inline('myModalLabel');
  3. CKEDITOR.inline('bodyModal');
  4.  
  5. $.fn.modal.Constructor.prototype.enforceFocus = function () {
  6. modal_this = this
  7. $(document).on('focusin.modal',function (e) {
  8. if (modal_this.$element[0] !== e.target && !modal_this.$element.has(e.target).length
  9. // add whatever conditions you need here:
  10. &&
  11. !$(e.target.parentNode).hasClass('cke_dialog_ui_input_select') && !$(e.target.parentNode).hasClass('cke_dialog_ui_input_text')) {
  12. modal_this.$element.focus()
  13. }
  14. })
  15. };

HTML代码

  1. <button type="button" data-toggle="modal" data-target="#modalAddBrand">Launch modal</button>
  2.  
  3. <div class="modal fade" id="modalAddBrand" tabindex="-1" role="dialog" aria-labelledby="modalAddBrandLabel" aria-hidden="true">
  4. <div class="modal-dialog modal-lg">
  5. <div class="modal-content">
  6. <div class="modal-header">
  7. <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
  8. <h4 class="modal-title" id="modalAddBrandLabel">add</h4>
  9.  
  10. </div>
  11. <div class="modal-body">
  12. <form>
  13. <textarea name="editor1" id="editor1" rows="10" cols="80">This is my textarea to be replaced with CKEditor.</textarea>
  14. </form>
  15. </div>
  16. <div class="modal-footer">
  17. <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
  18. <button id="AddBrandButton" type="button" class="btn btn-primary">Save</button>
  19. </div>
  20. </div>
  21. </div>
  22. </div>

的jsfiddle:

JSFiddle Example

有人能帮帮我吗?

@H_404_20@

解决方法

问题是,当你初始化CKEDITOR实例时,那个时间目标是隐藏的,所以如果你在目标显示后实例化编辑器它将会:

所以你简单地说:

  1. CKEDITOR.disableAutoInline = true;
  2.  
  3. $('#myModal').on('shown.bs.modal',function () {
  4. CKEDITOR.inline('myModalLabel');
  5. CKEDITOR.inline('bodyModal');
  6. });

但在关闭并重新打开模态后,您可能会收到错误

Uncaught The editor instance “myModalLabel” is already attached to the provided element

更新:

为此,我们可以有功能

  1. function ckCreate(name)
  2. {
  3. if(CKEDITOR.instances[name] === undefined)
  4. {
  5. CKEDITOR.inline(name);
  6. }
  7. }

只有在不存在的情况下才创建实例;

最后你的代码是:

  1. CKEDITOR.disableAutoInline = true;
  2.  
  3. $('#myModal').on('shown.bs.modal',function () {
  4. ckCreate('myModalLabel');
  5. ckCreate('bodyModal');
  6. });

最终小提琴:http://jsfiddle.net/0vLs3fku/4/

更新:需要销毁实例

  1. function ckCreate(name)
  2. {
  3. if (CKEDITOR.instances[name])
  4. {
  5. CKEDITOR.instances[name].destroy();
  6. }
  7.  
  8. CKEDITOR.inline(name);
  9. }
@H_404_20@ @H_404_20@

猜你在找的jQuery相关文章