简体   繁体   English

如何使用jQuery检测使用jWYSIWYG插件的文本区域内容的更改?

[英]How to detect a change with jQuery to the contents of a textarea that's using jWYSIWYG plugin?

I'm using the jwysiwyg plugin on textareas in my form (jwysiwyg: https://github.com/akzhan/jwysiwyg ) 我在表单中的textareas上使用jwysiwyg插件(jwysiwyg: https : //github.com/akzhan/jwysiwyg

I want to detect changes to any of the textareas that are in my form. 我想检测对表单中任何文本区域的更改。

Normally, with a standard non-wysiwyg textarea, I do: 通常,使用标准的非所见即所得文本区域,我可以:

$('textarea').change(function(e){
    alert('Something changed!');
 });

This proves ineffective when jwysiwyg is being used due to jwysiwyg's DOM manipulation. 由于jwysiwyg的DOM操作,在使用jwysiwyg时这被证明是无效的。

Anyone have any pointers? 有人有指针吗?

Use this code, I've tested it with the help/example/01.basic.html and 02.full.html, I guess it will work with the others too: 使用此代码,我已经使用help / example / 01.basic.html和02.full.html测试了它,我想它也可以与其他代码一起使用:

$(document).ready(function(){
var current = $("#wysiwyg-wysiwyg-iframe").contents().find('html');

     function detectFn(){
          alert('Detected');
       } 

       var timer;

    current.bind({    
                keyup: function(){
                    clearTimeout(timer);
          timer = setTimeout(detectFn, 2000)

                }
    });

});

The sources : 资料来源
Yasir Laghari and Ghommey have the real credit of this solution. Yasir Laghari和Ghommey确实对此解决方案感到满意。

jQuery/JavaScript: accessing contents of an iframe jQuery / JavaScript:访问iframe的内容

On keypress, when stop after X seconds call function 按键时,X秒后停止通话功能

Explanation : 说明

1) The 'jwysiwyg plugin' what it does is to replace the textarea for an iframe. 1)“ jwysiwyg插件”的作用是替换iframe的textarea。 The text is inside a 'p' tag, another thing to consider is that this tag can not be clicked, if you click the text, what you are actually clicking is the 'iframed html' tag. 文本位于“ p”标签内,要考虑的另一件事是无法单击此标签,如果单击文本,则实际上要单击的是“ iframed html”标签。

2) For jQuery to access and select an iframed element, the 'contents()' method must be used. 2)为了让jQuery访问并选择iframed元素,必须使用'contents()'方法。 That way we can start triggering some functions with the iframe. 这样,我们就可以开始使用iframe触发某些功能。

3) Create the detectFn() that will execute the code you want to use after detecting the change in the fake textarea. 3)在检测到假文本区域中的更改之后,创建将执行您要使用的代码的detectFn()。

4) Declare the variable 'timer'. 4)声明变量“ timer”。

5) The bind() method must be used with the 'iframed html' tag for using the keyup event. 5)bind()方法必须与“ iframed html”标记一起使用才能使用keyup事件。 That way the typing action of a textarea will be simulated. 这样,将模拟文本区域的键入动作。

6) Each keyup will execute the detect function multiple times, to prevent that, we use the setTimeout method and store the detectFn in the variable 'timer'. 6)每个keyup都会多次执行detect函数,为防止这种情况,我们使用setTimeout方法并将detectFn存储在变量“ timer”中。 Finally the variable must be passed to the clearTimeout as an argument, that way each keyup, will cancel the previous execution of the detect function. 最后,必须将变量作为参数传递给clearTimeout,这样,每次键入时都将取消对detect函数的先前执行。

I found this to be much simpler: 我发现这要简单得多:

    $('#editor_textarea').wysiwyg('document').keypress(function(e) {
        e.preventDefault(); //This will cancel the keypress
        alert('Keypress in jwysiwyg editor detected!');
    });

You can set autoSave to the true and do like these: 您可以将autoSave设置为true并执行以下操作:

$(element).wysiwyg('getContent').afterSave(function () { / magic / } $(element).wysiwyg('getContent')。afterSave(function(){/ magic /}

if you want you can trigger 'change' of an element in this handler 如果您愿意,可以触发此处理程序中元素的“更改”

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

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