如何使用 jquery/javascript 限制 ckeditor textarea

这是我的 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();

               }

        });

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

http://img.mukewang.com/61c41ec90001549608620516.jpg

白衣染霜花
浏览 184回答 1
1回答

翻翻过去那场雪

你可以这样做:ClassicEditor&nbsp; .create(document.querySelector('#QBQuestion'))&nbsp; .then(editor => {&nbsp; &nbsp; editor.editing.view.document.on('keydown', function(evt, data) {&nbsp; &nbsp; &nbsp; if (data.keyCode === 32 && $(data.domTarget).text().length === 0) {&nbsp; &nbsp; &nbsp; &nbsp; data.stopPropagation();&nbsp; &nbsp; &nbsp; &nbsp; data.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; evt.stop();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }, { priority: 'highest' });&nbsp; })&nbsp; .catch(error => {&nbsp; &nbsp; console.error(error);&nbsp; });演示ClassicEditor&nbsp; .create(document.querySelector('#QBQuestion'))&nbsp; .then(editor => {&nbsp; &nbsp; editor.editing.view.document.on('keydown', function(evt, data) {&nbsp; &nbsp; &nbsp; if (data.keyCode === 32 && $(data.domTarget).text().length === 0) {&nbsp; &nbsp; &nbsp; &nbsp; data.stopPropagation();&nbsp; &nbsp; &nbsp; &nbsp; data.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; evt.stop();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }, { priority: 'highest' });&nbsp; })&nbsp; .catch(error => {&nbsp; &nbsp; console.error(error);&nbsp; });<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>&nbsp; <div class="QB-PanelName">&nbsp; &nbsp; <lable>Question</lable>&nbsp; </div>&nbsp; <textarea name="QBQuestion" id="QBQuestion" rows="10" cols="80"></textarea></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript