ueditor使用editor.execCommand( 'link', {})插入鏈接無效的問題

介紹

因?yàn)轫?xiàng)目需要,所以要在百度富文本編輯器ueditor上二次開發(fā),有一個(gè)就是在富文本中插入鏈接的功能;

官方文檔如下:

execCommand(String cmd, Object options)
執(zhí)行當(dāng)前命令

參數(shù)列表
參數(shù)名 類型 描述
cmd String 命令字符串
options Object 設(shè)置自定義屬性,例如:url、title、target

示例

 editor.execCommand( 'link', '{
     url:'ueditor.baidu.com',
     title:'ueditor',
     target:'_blank'
 }' );

這里首先要糾正下,這里官方的代碼演示是錯(cuò)誤的,第二個(gè)參數(shù)應(yīng)該是一個(gè)對象,而不是字符串,所以,正確用法應(yīng)該是

 editor.execCommand( 'link', {
     url:'ueditor.baidu.com',
     title:'ueditor',
     target:'_blank'
 });

復(fù)現(xiàn)問題

當(dāng)插入鏈接(調(diào)用此方法)后,文本框沒有反應(yīng);
然后我們嘗試著去檢查富文本編輯框html代碼,會(huì)發(fā)現(xiàn):



鏈接加上了,但沒有文字,原來title字段是a 標(biāo)簽的title屬性。。。然而官方文檔也沒有詳細(xì)說明;

解決辦法

通過斷點(diǎn)查找,找到了 ueditor \ utf8-php \ dialogs \ link的link.html文件,如下有這么一段代碼:

function handleDialogOk(){
        var href =$G('href').value.replace(/^\s+|\s+$/g, '');
        if(href){
            if(!hrefStartWith(href,["http","/","ftp://",'#'])) {
                href  = "http://" + href;
            }
            var obj = {
                'href' : href,
                'target' : $G("target").checked ? "_blank" : '_self',
                'title' : $G("title").value.replace(/^\s+|\s+$/g, ''),
                '_href':href
            };
            //修改鏈接內(nèi)容的情況太特殊了,所以先做到這里了
            //todo:情況多的時(shí)候,做到command里
            if(orgText && text.value != orgText){
                link[browser.ie ? 'innerText' : 'textContent'] =  obj.textValue = text.value;
                range.selectNode(link).select()
            }
            if(range.collapsed){
                obj.textValue = text.value;
            }
            editor.execCommand('link',utils.clearEmptyAttrs(obj) );
            dialog.close();
        }
    }

結(jié)果測試,是obj.textValue起到了關(guān)鍵作用,所以,可以改為

 editor.execCommand( 'link', {
     url:'ueditor.baidu.com',
     title:'ueditor',
     target:'_blank',
     textValue:'ueditor'
 });

SUCCESS

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容