如何使用 jQuery 检测使用 jWYSIWYG 插件的文本区域内容的更改?
我在表单中的文本区域上使用 jwysiwyg 插件(jwysiwyg:https://github.com/akzhan/jwysiwyg< /a> )
我想检测表单中任何文本区域的更改。
通常,对于标准的非所见即所得文本区域,我这样做:
$('textarea').change(function(e){
alert('Something changed!');
});
由于 jwysiwyg 的 DOM 操作,当使用 jwysiwyg 时,这被证明是无效的。
有人有任何指点吗?
I'm using the jwysiwyg plugin on textareas in my form (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.
Anyone have any pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用此代码,我已经使用 help/example/01.basic.html 和 02.full.html 对其进行了测试,
我想它也可以与其他解决方案一起使用:
来源:
Yasir Laghari 和 Ghommey 拥有该解决方案的真正功劳。
jQuery/JavaScript:访问 iframe 的内容
按键时,X秒后停止时调用函数
说明:
1)“jwysiwyg 插件”的作用是替换 iframe 的 textarea。文本位于“p”标签内,另一件事要考虑的是该标签不能被单击,如果单击文本,则实际单击的是“iframed html”标签。
2) 对于 jQuery 访问和选择 iframed 元素,必须使用“contents()”方法。这样我们就可以开始使用 iframe 触发一些功能。
3) 创建 detectorFn(),它将在检测到假文本区域中的更改后执行您想要使用的代码。
4) 声明变量“timer”。
5)bind()方法必须与'iframed html'标签一起使用才能使用keyup事件。这样,将模拟文本区域的打字动作。
6)每个keyup都会多次执行Detect函数,为了防止这种情况,我们使用setTimeout方法并将DetectFn存储在变量'timer'中。最后,该变量必须作为参数传递给clearTimeout,这样每次按键都会取消先前执行的检测函数。
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:
The sources:
Yasir Laghari and Ghommey have the real credit of this solution.
jQuery/JavaScript: accessing contents of an iframe
On keypress, when stop after X seconds call function
Explanation:
1) The 'jwysiwyg plugin' what it does is to replace the textarea for an iframe. 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.
2) For jQuery to access and select an iframed element, the 'contents()' method must be used. That way we can start triggering some functions with the iframe.
3) Create the detectFn() that will execute the code you want to use after detecting the change in the fake textarea.
4) Declare the variable 'timer'.
5) The bind() method must be used with the 'iframed html' tag for using the keyup event. 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'. 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.
我发现这要简单得多:
I found this to be much simpler:
您可以将 autoSave 设置为 true 并执行以下操作:
$(element).wysiwyg('getContent').afterSave(function () { /magic/ }
如果您愿意,可以触发“更改”此处理程序中的元素的
You can set autoSave to the true and do like these:
$(element).wysiwyg('getContent').afterSave(function () { /magic/ }
if you want you can trigger 'change' of an element in this handler