How to use CKEditor in a Bootstrap Modal?

后端 未结 14 1171
名媛妹妹
名媛妹妹 2020-11-30 04:14

If I use the CKEditor plugin in an HTML page based on a Bootstrap template, it works great, however if I insert the editor on a Bootstrap Modal like this

<         


        
相关标签:
14条回答
  • 2020-11-30 04:32

    Use the 100% working script..

    <script type="text/javascript">
        // Include this file AFTER both jQuery and bootstrap 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_textarea')
            && !$(e.target.parentNode).hasClass('cke_dialog_ui_input_text')) {
              modal_this.$element.focus()
            }
          })
        };
    </script>
    

    Thanks!

    0 讨论(0)
  • 2020-11-30 04:33

    For Bootstrap 4, this line will no longer work. They will need to figure out that it changed to:

    $.fn.modal.Constructor.prototype._enforceFocus = function() {};
    

    Instead of

    $.fn.modal.Constructor.prototype.enforceFocus = function() {};
    

    So, Code should be like this:

    $.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()
              }
            })
          };
    
    0 讨论(0)
  • 2020-11-30 04:34

    I just remove the tabindex="-1" from the dialog main div element. Here is the sample code

    <div class="modal fade" id="bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
    

    I just remove this

    tabindex="-1"
    

    and it starts working.

    0 讨论(0)
  • 2020-11-30 04:36
      $(document).on({'show.bs.modal': function () {
                     $(this).removeAttr('tabindex');
          } }, '.modal');
    
    0 讨论(0)
  • 2020-11-30 04:37

    I was getting Uncaught TypeError: Cannot read property 'fn' of undefined

    So I just replaced the $ inside the script provided by @Pavel Kovalev above to jQuery.

    jQuery.fn.modal.Constructor.prototype.enforceFocus = function () {
        modal_this = this
        jQuery(document).on('focusin.modal', function (e) {
            if (modal_this.$element[0] !== e.target && !modal_this.$element.has(e.target).length
                    && !jQuery(e.target.parentNode).hasClass('cke_dialog_ui_input_select')
                    && !jQuery(e.target.parentNode).hasClass('cke_dialog_ui_input_text')) {
                modal_this.$element.focus()
            }
        })
    };
    
    0 讨论(0)
  • 2020-11-30 04:41

    This should help http://jsfiddle.net/pvkovalev/4PACy/

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

    Update October 2016:

    CDN link for CKEditor has been changed, so I updated jsfiddle

    0 讨论(0)
提交回复
热议问题