最近在某项目中引用了ueditor,并没有注意到除了文本编辑与上传图片以外的功能是否完好。后面测试提了俩bug,发现超链接与预览无效的问题,业务需求确实需要这俩功能,还是得解决。

在点击超链接或预览按钮的时候,发现控制台报错:Uncaught ReferenceError: editor is not undefined at link.html,大致就是link里面引用的所有变量都Undefined。

于是博主打开link.html文件,发现头部调用了internal.js,那该页面引用的所有变量应该也在这个文件声明了。

(function () {
    var parent = window.parent;
    //dialog对象
    dialog = parent.$EDITORUI[window.frameElement.id.replace( /_iframe$/, '' )];
    //当前打开dialog的编辑器实例
    editor = dialog.editor;

    UE = parent.UE;

    domUtils = UE.dom.domUtils;

    utils = UE.utils;

    browser = UE.browser;

    ajax = UE.ajax;

    $G = function ( id ) {
        return document.getElementById( id )
    };
    //focus元素
    $focus = function ( node ) {
        setTimeout( function () {
            if ( browser.ie ) {
                var r = node.createTextRange();
                r.collapse( false );
                r.select();
            } else {
                node.focus()
            }
        }, 0 )
    };
    ...省略

})();

$(function(){...})()虽然能够立即执行,但是里面的变量在外面是获取不到的,所以博主把这层直接去掉。

var parent = window.parent;
//dialog对象
dialog = parent.$EDITORUI[window.frameElement.id.replace( /_iframe$/, '' )];
//当前打开dialog的编辑器实例
editor = dialog.editor;

UE = parent.UE;

domUtils = UE.dom.domUtils;

utils = UE.utils;

browser = UE.browser;

ajax = UE.ajax;

$G = function ( id ) {
    return document.getElementById( id )
};
//focus元素
$focus = function ( node ) {
    setTimeout( function () {
        if ( browser.ie ) {
            var r = node.createTextRange();
            r.collapse( false );
            r.select();
        } else {
            node.focus()
        }
    }, 0 )
};
...省略

改掉之后看到超链接和预览都可以了,但是打开ie一看,咦,怎么回事,你咋不行呢。。。再次看控制台一堆变量无法定义。。。

又一层层找,遂又发现上面代码行声明的 var parent = window.parent;  parent是undefined,,第一行都不行,下面这些变量肯定全找不着了。

最后试着把window.parent改成window.top,惊喜的发现竟然可以了。

var parent = window.top;
//dialog对象
dialog = parent.$EDITORUI[window.frameElement.id.replace( /_iframe$/, '' )];
//当前打开dialog的编辑器实例
editor = dialog.editor;

UE = parent.UE;

domUtils = UE.dom.domUtils;

utils = UE.utils;

browser = UE.browser;

ajax = UE.ajax;

$G = function ( id ) {
    return document.getElementById( id )
};
//focus元素
$focus = function ( node ) {
    setTimeout( function () {
        if ( browser.ie ) {
            var r = node.createTextRange();
            r.collapse( false );
            r.select();
        } else {
            node.focus()
        }
    }, 0 )
};
...省略

完结,撒花!

 posted on 2021-04-26 14:11  一个大柚子~  阅读(750)  评论(0编辑  收藏  举报