javascript - 关键事件不适用于多个 ckeditor

标签 javascript jquery ckeditor

我有这个jsfiddle 当用户在 ckeditor 上输入过滤词时,这里会提醒用户。在我的示例中,过滤词 是 ants 和 words。因此,如果您输入这些词,它将提醒用户。

html

<input type="textarea" id="editor1"/>
<div id="dest"></div>

js

var filter = ['ants', 'words'], // our list of words
    regAry = new Array(), // we'll create one regex per word for testing
    alertedWords = new Array(), // keep track of how many words there were at the last alert, for each word
    reg = new RegExp("(/s" + filter.join("|") + "/s)", "g"); // one regex to rule them all!

for(var f in filter) { // setup...
    regAry[f] = new RegExp(filter[f], "g"); // one regex per word for testing
    alertedWords[f] = 0; // no alerts yet, so 0 for each word
}
var editor = CKEDITOR.replace( 'editor1' );
//var value = CKEDITOR.instances['editor1'].getData();
//alert(value);

 editor.on('contentDom', function() {
 editor.document.on('keyup', function(event) {

for(var index in regAry) { // loop over our list of words
    var value = CKEDITOR.instances['editor1'].getData();
       var test = value.match(regAry[index]); // test how many times this word appears
       if( test && test.length > alertedWords[index] ) // if it appears more than the last time we alerted...
       {
           alert("The following word/words  "+ CKEDITOR.instances['editor1'].getData().match(regAry[index])+" is banned"); // do an alert!
       }
        alertedWords[index] = (test ? test.length : 0); // update the word count for this word
    } // keep looping     

 });
 });

现在如果我有 2 个或更多像 this 这样的 ckeditor,我的问题就出现了 它似乎不起作用。虽然出现了编辑器,但没有出现警报。

html

<input type="textarea" id="editor1"/>
<input type="textarea" id="editor2"/>
<div id="dest"></div>

js

var filter = ['ants', 'words'], // our list of words
    regAry = new Array(), // we'll create one regex per word for testing
    alertedWords = new Array(), // keep track of how many words there were at the last alert, for each word
    reg = new RegExp("(/s" + filter.join("|") + "/s)", "g"); // one regex to rule them all!

for(var f in filter) { // setup...
    regAry[f] = new RegExp(filter[f], "g"); // one regex per word for testing
    alertedWords[f] = 0; // no alerts yet, so 0 for each word
}

for(var i=1;i<3;i++){
var editor = CKEDITOR.replace( 'editor'+i );
//var value = CKEDITOR.instances['editor1'].getData();
//alert(value);

 editor.on('contentDom', function() {
 editor.document.on('keyup', function(event) {

for(var index in regAry) { // loop over our list of words
    var value = CKEDITOR.instances['editor'+i].getData();
       var test = value.match(regAry[index]); // test how many times this word appears
       if( test && test.length > alertedWords[index] ) // if it appears more than the last time we alerted...
       {
           alert("The following word/words  "+ CKEDITOR.instances['editor'+i].getData().match(regAry[index])+" is banned"); // do an alert!
       }
        alertedWords[index] = (test ? test.length : 0); // update the word count for this word
    } // keep looping     

 });
 });
}

怎么办?

最佳答案

不要用 for 循环来做,而是用 jquery each 来做,比如:

$( 'input[type=textarea]').each( function(indx) {

  var editor = CKEDITOR.replace( $(this).attr('id') );

  .....

FIDDLE

关于javascript - 关键事件不适用于多个 ckeditor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23126352/

相关文章:

php - XMLHttpRequest 适用于 FireFox,不适用于 Safari

javascript - JQuery width() 返回错误值?

javascript - 将 Owl Carousel 集成到 Wordpress 中

javascript - 基于多个单选选项显示字段

jquery - 在显示 div 时它总是显示在页面顶部

c# - 使用 ASP.NET WebAPI 返回 bool 值以供 Ajax GET 请求使用

events - CKEDITOR.setData 防止使用 .on 函数附加事件

javascript - CKEditor 中的 allowedContent 和 extraAllowedContent 有什么区别?

javascript - bbcode ckeditor 对齐插件

javascript - 文本左对齐问题