【问题标题】:Bootstrap with CKEditor equals problems带有 CKEditor 的引导程序等于问题
【发布时间】:2013-01-03 10:12:18
【问题描述】:

我正在尝试创建一个包含 CKEditor 实例的 Bootstrap 模态,但是有很多问题...

所以基本上这些字段未启用,它们看起来不像,但我无法与它们交互。有没有人可以解决这种奇怪的行为?

【问题讨论】:

  • 我遇到了类似的问题 - 工具栏按钮有效,但其中的文本字段不可编辑!你有没有找到解决这个问题的方法?我见过一些 z-index hack,但似乎都没有。

标签: twitter-bootstrap ckeditor z-index field instance


【解决方案1】:

FWIW,我无法让 Peter's solution 工作,但以下内容对我有用,并且仍然将 hack 保存在单独的文件中,因此您不必编辑任何 Bootstrap 源文件:

// bootstrap-ckeditor-modal-fix.js
// hack to fix ckeditor/bootstrap compatiability bug when ckeditor appears in a bootstrap modal dialog
//
// Include this AFTER both bootstrap and ckeditor are loaded.

$.fn.modal.Constructor.prototype.enforceFocus = function() {
  modal_this = this
  $(document).on('focusin.modal', function (e) {
    if (modal_this.$element[0] !== e.target && !modal_this.$element.has(e.target).length 
    && !$(e.target.parentNode).hasClass('cke_dialog_ui_input_select') 
    && !$(e.target.parentNode).hasClass('cke_dialog_ui_input_text')) {
      modal_this.$element.focus()
    }
  })
};

【讨论】:

  • 谢谢。很好的解决方案)
  • 适用于 4.5.5 和 Boostrap 2.x。谢谢! :)
  • 适用于 Bootstrap 3.3.2 和 CKEditor 4.6.1。谢谢:)
  • 在应用 .js hacks 之前,向下滚动到 @andrew-kapunin 答案。快速测试表明,删除tabindex 的简单标记更改可能就足够了。
【解决方案2】:

我刚刚从模态容器中删除了tabindex 属性,这似乎为我解决了这个问题。

这是 fat 在此处建议的:https://github.com/twbs/bootstrap/issues/6996

【讨论】:

  • 这是引导程序的最佳和最简单的解决方案。只需从模态
    属性中删除 'tabindex="-1" '。
【解决方案3】:

如果上述所有解决方案都不适合您,请尝试以下操作:

   $('#myModal').on('shown.bs.modal', function() {
        $(document).off('focusin.modal');
    });

它立即对我有用。以下是来源:tobiv's answer - github

【讨论】:

  • 太棒了,没有一个解决方案对我有用,但这个...谢谢分享。
  • 更新:这只是覆盖了 tabindex="-1"。
【解决方案4】:

我没有弄乱 Bootstrap 源,而是重新附加了焦点事件处理程序。

我查看了 Bootstrap 模态(未缩小)代码,以找到在 Modal.enforceFocus() 下定义事件处理程序的位置:

var that = this
$(document).on('focusin.modal', function (e) {
  if (that.$element[0] !== e.target && !that.$element.has(e.target).length) {
    that.$element.focus()
  }
})

然后我向 CKEditor 添加了一个方法来修改此功能。你可以把它放在任何地方;可能在一个仅用于 CKEditor 覆盖的文件中。

CKEDITOR.bootstrapModalFix = function (modal, $) {
  modal.on('shown', function () {
    var that = $(this).data('modal');
    $(document)
      .off('focusin.modal')
      .on('focusin.modal', function (e) {
        // Add this line
        if( e.target.className && e.target.className.indexOf('cke_') == 0 ) return;

        // Original
        if (that.$element[0] !== e.target && !that.$element.has(e.target).length) {
          that.$element.focus()
        }
      });
  });
};

所以现在,如果我知道我要在 Bootstrap 模式中加载一个 CKEditor,我会调用这个方法,假设 jQuery 是 $

CKEDITOR.bootstrapModalFix( $('#myModal'), $ )

【讨论】:

    【解决方案5】:

    嘿,我遇到了这些问题。我发现这张票 https://github.com/twitter/bootstrap/issues/6996 似乎解决了我无法选择输入的问题。我将这张票的更改扩展到:

    if (that.$element[0] !== e.target && !that.$element.has(e.target).length && !$(e.target.parentNode).hasClass('cke_dialog_ui_input_select') && !$(e.target.parentNode).hasClass('cke_dialog_ui_input_text')){
    

    这允许选择和输入一样可用,尽管重复选择器有点麻烦,但它确实修复了错误。希望对您有所帮助。

    【讨论】:

    • 为什么不使用原始修复hasClass('cke')
    • 寻求解决此问题的任何人都应该使用 Peter 的答案而不是我建议的答案,因为这意味着您不必篡改源代码。
    【解决方案6】:

    bootstrap modal 的 z-index 高于 ckeditor 面板的 z-index。因此,我发现的另一种解决方案是增加 ckeditor 的 z-index。将以下行添加到 ckeditor config.js

    // app/assets/javascripts/ckeditor/config.js
    config.baseFloatZIndex = 1E5;
    

    【讨论】:

      【解决方案7】:

      我在 React-Boostrap 模态中使用 CKEditor。我在 Wiris Mathtype 编辑器中遇到了焦点问题。我尝试了以下两种方法来解决我的问题。

      Modal 组件加载时粘贴以下脚本

      document.getElementsByClassName('modal')[0].removeAttribute('tabindex')
      

      将此属性添加到模态组件

      enforceFocus={false}
      

      【讨论】:

        【解决方案8】:

        工作示例在这里:http://jsfiddle.net/pvkovalev/4PACy/

                $.fn.modal.Constructor.prototype.enforceFocus = function () {
                    modal_this = this
                    $(document).on('focusin.modal', function (e) {
                        if (modal_this.$element[0] !== e.target && !modal_this.$element.has(e.target).length
                        // add whatever conditions you need here:
                        &&
                        !$(e.target.parentNode).hasClass('cke_dialog_ui_input_select') && !$(e.target.parentNode).hasClass('cke_dialog_ui_input_text')) {
                            modal_this.$element.focus()
                        }
                    })
                };
        

        【讨论】:

          【解决方案9】:

          Bootstrap 将 focusin.modal 更改为 shown.bs.modal

           $.fn.modal.Constructor.prototype.enforceFocus = function() {
            modal_this = this
            $(document).on('shown.bs.modal', function (e) {
              if (modal_this.$element[0] !== e.target && !modal_this.$element.has(e.target).length 
              && !$(e.target.parentNode).hasClass('cke_dialog_ui_input_select') 
              && !$(e.target.parentNode).hasClass('cke_dialog_ui_input_text')) {
                modal_this.$element.focus()
              }
            })
          };
          

          这对我有用。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-09-20
            • 1970-01-01
            • 2018-01-01
            • 1970-01-01
            • 2019-02-12
            • 2011-05-07
            • 2012-07-21
            • 1970-01-01
            相关资源
            最近更新 更多