TinyMCE 在调用 execCommand 时窃取焦点

问题描述

我的 HTML 页面上有一堆输入,还有一个被 TinyMCE 编辑器替换的 textarea。加载此页面的情况有多种,并且每次其中一个输入都必须获得焦点。但是当 TinyMCE 被初始化时,它只是偷走了焦点。我将问题缩小到 execCommand 调用,该调用在 TinyMCE 中设置默认字体大小。

这个问题可以在这个简化的代码中重现:

<!DOCTYPE html>
<html>
    <head>
        <script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script>
        <script>
            tinymce.init({
                selector: 'textarea',setup: function(editor) {
                    editor.on('init',function() {
                        this.execCommand("fontSize",false,"14pt"); // <<---- This line causes the TinyMCE editor to steal the focus!
                    });
                }
            });
        </script>
    </head>

    <body>
        <input id="inp" />
        <textarea>Hello,World!</textarea>
        
        <script>
            document.getElementById("inp").focus();
        </script>
    </body>
</html>

我在 Firefox、Chrome 和 Edge 中对此进行了测试,并且在所有这些中都发生了相同的问题。知道如何防止这种情况吗?一旦 TinyMCE 完成初始化,我不想再次设置焦点,因为此时页面已滚动到错误的位置。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)