检测 WordPress 编辑帖子页面的富文本编辑器 (tinyMCE) 中的更改事件

发布于 2024-11-29 11:45:04 字数 480 浏览 4 评论 0原文

如何检测“编辑帖子”页面中的富文本编辑器发生更改?

我想要的是当用户在富文本编辑器(HTML 和可视化窗格)中键入内容时执行一个函数。

编辑:我在 HTML 窗格中完成了与tinyMCE 创建的隐藏文本区域中的 jQuery keyup 事件关联,但我仍然无法检测到 Visual 选项卡中的用户输入。

我正在使用 Wordpress 3.2.1,并且我知道他们正在使用tinyMCE 来实现富文本编辑器功能。

似乎 tinyMCE 使用 iframe 在富文本编辑器中构建 Visual 选项卡,所以我尝试了指出的示例 此处没有任何结果。

任何帮助表示赞赏。

How I can detect that there was a change in the rich text editor in the Edit Post page?

What I want is to execute a function when the user typed something in the rich text editor (HTML and Visual panes).

EDIT: I accomplished that in the HTML pane associating a jQuery keyup event in the hidden textarea that tinyMCE create, but still I've not been able to detect user input in the Visual tab.

I'm using Wordpress 3.2.1 and I know that they are using tinyMCE for the rich text editor functionality.

Seems that tinyMCE uses an iframe to build the Visual tab in the rich text editor, so I tried the examples pointed here without any results.

Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝海 2024-12-06 11:45:04

最后,我能够通过添加以下代码片段来解决这个问题:

<script type="text/javascript">
    jQuery(function ($) {
        // Was needed a timeout since RTE is not initialized when this code run.
        setTimeout(function () {
            for (var i = 0; i < tinymce.editors.length; i++) {
                tinymce.editors[i].onChange.add(function (ed, e) {
                    // Update HTML view textarea (that is the one used to send the data to server).
                    ed.save();
                });
            }
        }, 1000);
    });
</script>

感谢@HarkályGergő 的提示来解决这个问题:)

Finally I was able to solve this issue by adding this code snippet:

<script type="text/javascript">
    jQuery(function ($) {
        // Was needed a timeout since RTE is not initialized when this code run.
        setTimeout(function () {
            for (var i = 0; i < tinymce.editors.length; i++) {
                tinymce.editors[i].onChange.add(function (ed, e) {
                    // Update HTML view textarea (that is the one used to send the data to server).
                    ed.save();
                });
            }
        }, 1000);
    });
</script>

Thannks to @HarkályGergő for his prompting to solve this question :)

可是我不能没有你 2024-12-06 11:45:04

不知道如何将其添加到 WordPress,但从 TinyMCE 的角度来看 编辑器上的 isDirty() 方法应该有所帮助。

因此,您可以简单地使用

if (tinyMCE.activeEditor.isDirty()) {
  //Do something
}

来检查当前活动的编辑器。 WordPress 可能会为您提供访问编辑器实例的替代方法。

Not sure how to add this to WordPress, but from a TinyMCE perspective the isDirty() method on the editor should help.

So you could simply use

if (tinyMCE.activeEditor.isDirty()) {
  //Do something
}

to check the currently active editor. WordPress may give you alternative ways to access the editor instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文