【问题标题】:How to trigger the blur event on CKEditor textareas?如何触发CKEditor textareas上的模糊事件?
【发布时间】:2015-09-28 20:57:08
【问题描述】:

我们有一些代码用于验证在模糊上运行的 CKEditor textarea 上的输入。我们将ckeditor_textarea 类添加到所有使用CKEditor 的文本区域并运行此代码以将必要的函数附加到模糊事件:

$("textarea.ckeditor_textarea").each(function(){
    var textarea_id = $(this).attr("id");
    CKEDITOR.instances[textarea_id].on('blur',function(){
        // Validation functions here
    });
});

这可以在模糊事件发生时触发验证函数。但是我们还需要在提交前手动触发提交按钮时的模糊事件,以便在提交前对这些CKEditor文本区域运行验证功能。

如何触发 CKEditor textarea 上的模糊事件?使用 jQuery 语法(这当然不起作用,因为 CKEditor 实例不是 jQuery 对象),我基本上是在寻找这样的东西:

$("textarea.ckeditor_textarea").each(function(){
    var textarea_id = $(this).attr("id");
    CKEDITOR.instances[textarea_id].trigger('blur');
});

【问题讨论】:

    标签: javascript jquery ckeditor


    【解决方案1】:
    1. 您不应该混合使用 jQuery 事件和 CKEDITOR 事件。如果您想对 CKEDITOR 实例进行模糊处理,请注册它:

      ckeInst.on('模糊',函数(e){

      });

    然后,如果你真的想触发模糊事件,你可以这样做:

    ckeInst.focusManager.blur( true ); 
    

    从事件中检索编辑器(如果有的话),或者通过 CKEDITOR.instances['yourCkeName'];

    【讨论】:

      【解决方案2】:

      对于提交验证,我建议在您的提交处理程序中使用updateElement() 方法,然后运行您的验证代码:

      以下将使用页面上的编辑器更新所有元素:

      $('form').on('submit', function(e){
           for (instance in CKEDITOR.instances) {
               CKEDITOR.instances[instance].updateElement();
            }
            // run validation code
      
      });
      

      这也确保表单数据与编辑器本身保持同步

      【讨论】:

      • 赞成投票,因为这在某些情况下可能是一个很好的解决方案,updateElement() 是一个很好的观点。不过,这并不是我想要的,但这足以让我找到适合我情况的解决方案。 (已发布答案。)
      【解决方案3】:

      根据@charlietfl 的回答,我能够针对我的情况提出解决方案。

      首先,我创建了一个运行验证代码的函数来调用:

      function ckeditor_blur_event(textarea_id){
          // validation code  
      }
      

      然后,我将问题中的第一段代码更改为使用新功能:

      $("textarea.ckeditor_textarea").each(function(){
          var textarea_id = $(this).attr("id");
          ckeditor_blur_event(textarea_id)
      });
      

      最后,我在提交表单的时候触发了同样的验证码:

      $("textarea.ckeditor_textarea").each(function(){
          var textarea_id = $(this).attr("id");
          CKEDITOR.instances[textarea_id].updateElement();
          ckeditor_blur_event(textarea_id)
      });
      

      【讨论】:

        【解决方案4】:

        对我来说可靠且正确工作的唯一方法是:

        editor.window.$.frameElement.blur();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多