如何在ckeditor中使用onKeyUp函数

时间:2018-05-07 08:26:26

标签: javascript jquery ckeditor

我在页面中使用CKeditor,并尝试计算KeyUp方法中剩余的字符。

这是我的代码

HTML

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Restaurant>()
            .HasMany(p => p.Cuisines)
            .WithMany(r => r.Restaurants)
            .Map(mc =>
            {
                mc.MapLeftKey("RestaurantId");
                mc.MapRightKey("CuisineId");
                mc.ToTable("RestaurantCuisines");
            });
     }
}

使用CKeditor替换它也设置工具栏

<div class="row">
  <div class="col-xs-12 col-sm-6 inputBlock textareaRegion">
  <div id="editor1" class="textarea"></div>
  <label class="showLabel">Comments</label><div class="maxlengthCountDiv">Remaining character length:<span class="maxlengthCount"></span></div>
</div>
<div class="row">
  <div class="col-xs-12 col-sm-6 inputBlock textareaRegion">
  <div id="editor2" class="textarea"></div>
  <label class="showLabel">Comments</label><div class="maxlengthCountDiv">Remaining character length:<span class="maxlengthCount"></span></div>
</div>

javascript代码

  CKEDITOR.replace( 'editor1' );
  CKEDITOR.replace( 'editor2' );

CKEDITOR.config.toolbar = [
       ['Styles','Format','Font','FontSize'],
       ['Bold','Italic','Underline','StrikeThrough','-','Undo','Redo','-','Cut','Copy','Paste','Find','Replace','-'],
       ['NumberedList','BulletedList'],
    ] ;

没有解决KeyUp函数。如何修复此问题?

3 个答案:

答案 0 :(得分:2)

除了@Artem Dorodovskiy(这是推荐的方法)之外,您还可以将keyupkeydown事件直接附加到editable,如下所示:

var editor = CKEDITOR.replace( 'editor1', {});
editor.on( "pluginsLoaded", function( event ){
    editor.on( 'contentDom', function( evt ) {
        var editable = editor.editable();                   
        editable.attachListener( editable, 'keyup', function( e ) { 
            // do something
        });
    }); 
});

答案 1 :(得分:1)

根据文件event-key,事件应该是这样的

editor.on( 'key', function( evt ) {
  if ( evt.data.keyCode == CKEDITOR.CTRL + 90 ) {
    // Do something...

    // Cancel the event, so other listeners will not be executed and
    // the keydown's default behavior will be prevented.
    evt.cancel();
  }
} );

答案 2 :(得分:0)

试试这个

CKEDITOR.instances.editor1.on('key', function(evt) { 
                console.log(evt.data.keyCode);
            });

CKEDITOR.instances.editor2.on('key', function(evt) { 
                console.log(evt.data.keyCode);
            });