如何使用 jquery/javascript 在 ckeditor textarea 的第一个位置限制 space

How to restrict space at first position in ckeditor textarea using jquery/javascript

这是我的 ckeditor div 代码

<div>
    <div class="QB-PanelName"><lable>Question</lable></div>
    <textarea name="QBQuestion" id="QBQuestion" rows="10" cols="80"></textarea>                 
 </div>

这是 javascript/jquery 代码 这是用于 CKeditor http://cdn.ckeditor.com/4.13.0/basic/ckeditor.js

的 cdn
CKEDITOR.replace('QBQuestion');

     CKEDITOR.instances.QBQuestion.on('key', function (evt) {
            var kc = evt.data.keyCode;
            console.log(evt);
            if (kc === 32) {
                    event.preventDefault();
               }
        });

将 space 限制在第一个位置是行不通的。 我已经更新了 jquery 代码,但是 kc===32 完全不允许 space 但我的要求是第一位,我不想 space

你可以这样做:

ClassicEditor
  .create(document.querySelector('#QBQuestion'))
  .then(editor => {
    editor.editing.view.document.on('keydown', function(evt, data) {
      if (data.keyCode === 32 && $(data.domTarget).text().length === 0) {
        data.stopPropagation();
        data.preventDefault();
        evt.stop();
      }
    }, { priority: 'highest' });
  })
  .catch(error => {
    console.error(error);
  });

演示

ClassicEditor
  .create(document.querySelector('#QBQuestion'))
  .then(editor => {
    editor.editing.view.document.on('keydown', function(evt, data) {
      if (data.keyCode === 32 && $(data.domTarget).text().length === 0) {
        data.stopPropagation();
        data.preventDefault();
        evt.stop();
      }
    }, { priority: 'highest' });
  })
  .catch(error => {
    console.error(error);
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://cdn.ckeditor.com/ckeditor5/15.0.0/classic/ckeditor.js"></script>
<div>
  <div class="QB-PanelName">
    <lable>Question</lable>
  </div>
  <textarea name="QBQuestion" id="QBQuestion" rows="10" cols="80"></textarea>
</div>