简体   繁体   English

jQuery文本编辑器(jqte),使用change事件

[英]jQuery Text Editor (jqte), using change event

I am trying to write some code for change() event using jQuery Text Editor (jqte) , I have two functions which give jqte functionality to textarea's 我正在尝试使用jQuery文本编辑器(jqte)change()事件编写一些代码,我有两个函数将jqte功能赋予textarea的

One for editors loaded with Javascript, when clicking some elements in a page 单击页面中的某些元素时,一个用于装载Javascript的编辑器

function onLoadEditor(){
    jQuery(".comment-editor").jqte({
        // some jqte params, such as fsize: false,indent: false...
        change: function(){ observeEditor(); }
    });
}

And other, generic function, for pages with one single editor 其他通用功能,适用于具有一个编辑器的页面

jQuery(function() {
    jQuery(".comment-editor").jqte({
        // some jqte params, such as fsize: false,indent: false...
        change: function(){ observeEditor(); }
    });
});

I want to access the id of the concrete textarea (all textareas in the page have an id) which has fired the change() event 我想访问触发了change()事件的具体textarea的ID(页面中的所有textarea都有一个ID)。

How should I write observeEditor() function to achieve this? 我应该如何编写observeEditor()函数来实现这一目标? Or... how I should define the function in jqte change property? 或者...我应该如何在jqte change属性中定义函数?

After reading this jQuery blur event with ID and value I have solved it, with following code (simplified) 在阅读完具有ID和值的jQuery模糊事件之后,我已经解决了,下面的代码(简化了)

function onLoadEditor(){
    jQuery(".comment-editor").each(function(idx, elem) {
        jQuery(this).jqte({
            // some jqte params, such as fsize: false,indent: false...
            change: observeEditor(elem.id),
    });
}

jQuery(function() {
    onLoadEditor();
});

But now I have another problem... 但是现在我有另一个问题...

As you can read in the original question, onLoadEditor() is called when clicking some elements in a page. 正如您在原始问题中可以看到的那样,单击页面中的某些元素时将调用onLoadEditor() Then another javascript function jsComment() is called, builds a form (with a textarea.comment-editor field included) and it is rendered this way 然后调用另一个javascript函数jsComment() ,构建一个表单(包含textarea.comment-editor字段)并以这种方式呈现

function jsComment(){
    ...
    var form = '<div class="comments_wrapper ... ';
    jQuery(form).insertAfter(some_element).fadeIn('fast');
    onLoadEditor();
}

Problem is change() event is being fired only once, when form fades in, while the idea is the opposite, event should fire when user adds some text, not when appearing... Any tips? 问题是change()事件仅在表单淡入时被触发一次,而想法恰恰相反,当用户添加一些文本而不是在出现时该事件应触发……任何提示?

UPDATE After reading Event binding on dynamically created elements? 更新阅读动态创建的元素上的事件绑定之后 I have solved it this way 我已经解决了

function onLoadEditor(){
    jQuery('.comment-editor').each(function(idx, elem) {
        jQuery(this).jqte({
            // some jqte params, such as fsize: false,indent: false...
        });
        jQuery(document).on('change', 
            jQuery('.comment-editor'), 
            function(){ 
                observeEditor(elem.id); 
            }
        );
    });
}

jQuery(function() {
    onLoadEditor();
});

Although finally I am not using change() event, as it was being fired constantly. 尽管最后我没有使用change()事件,因为它一直被触发。 Performing better with keyup() & paste() , for instance 例如,在keyup()paste()表现更好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM