0

我在 IE 和 Firefox 中使用 nicEdit 链接创建工具有问题。

一般来说,我认为问题与execCommandIE和FireFox有关。执行后似乎文档没有更新execCommand

这是我的 nicEdit create link 命令问题的一个示例。

if(!this.ln) {
        var tmp = 'javascript:nicTemp();';
        this.ne.nicCommand("createlink",tmp);
        this.ln = this.findElm('A','href',tmp);
        // set the link text to the title or the url if there is no text selected
        alert(this.ln);
        if (this.ln.innerHTML == tmp) {
            this.ln.innerHTML = this.inputs['title'].value || url;
        };
    }

上面的代码在没有选择文本的情况下被调用,Chrome返回'javascript:nicTemp()'alert(this.ln)而IE 8和Firefox返回,所以IE和Firefox中遇到错误'undefined'的下一行。alert

似乎findElem找不到新创建的链接nicCommand,依次调用execCommand

当我尝试查找和修改使用 创建的标签时,我遇到了类似的问题execCommand,似乎 dom 没有更新以包含它们。

我对吗?我怎么解决这个问题?如何强制更新文档....

请帮忙

4

3 回答 3

2

在没有选择文本的情况下,我对 nicEdit 的技巧是将通过“添加链接”表单给出的标题粘贴到文档中并选择它,然后其余代码在选择文本时工作。

我使用以下链接中描述的函数 pasteHtmlAtCaret 来粘贴标题

在 contenteditable div 中的插入符号处插入 html

this.removePane();
var url = this.inputs.href.value;
var selected = getSelected();
var B= 'javascript:nicTemp()';
if (selected == '')
{
    var B = url;
    pasteHtmlAtCaret(this.inputs['title'].value || url,true);
}                   
if(!this.ln){
    this.inputs.title.value;this.ne.nicCommand("createlink",B);
    this.ln=this.findElm("A","href",B)
}

getSelected 也是一个简单的函数,如下所示

function getSelected()
{
    if (document.selection)
        return document.selection.createRange().text;
    else
        return window.getSelection();
}
于 2013-08-24T18:23:50.143 回答
1

艾哈迈德,只需使用“提交”功能的这种变体来避免链接的“插入/编辑”问题,它对我有用:

submit : function(e) {
    var url = this.inputs['href'].value;
    if(url == "http://" || url == "") {
        alert("Introduce una URL valida para crear el Link.");
        return false;
    }
    this.removePane();

    if(!this.ln) {
        //**************** YOUR CHANGE WITH A BIT OF VARIATION **************
        var selected = this.getSelected();
        var tmp = 'javascript:void(0)';
        if (selected == '') {
            tmp = url;
            this.pasteHtmlAtCaret(this.inputs['title'].value || tmp, true);
        }
        //**************** END OF YOUR CHANGE WITH A BIT OF VARIATION **************

        this.ne.nicCommand("createlink",tmp);
        this.ln = this.findElm('A','href',tmp);

        // set the link text to the title or the url if there is no text selected
        if (this.ln.innerHTML == tmp) {
            this.ln.innerHTML = this.inputs['title'].value || url;
        };
    }

    if(this.ln) {
        var oldTitle = this.ln.title;
        this.ln.setAttributes({
            href: this.inputs['href'].value,
            title: this.inputs['title'].value,
            target: '_blank'
        });
        // set the link text to the title or the url if the old text was the old title
        if (this.ln.innerHTML == oldTitle) {
            this.ln.innerHTML = this.inputs['title'].value || this.inputs['href'].value;
        };
    }
}
于 2014-04-07T06:53:20.103 回答
0
    this.removePane();
            var url = this.inputs['href'].value;
            var selected = getSelected();
            var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
            var tmp = "";
            if(isChrome == true){
                tmp=url;
            }
            else{tmp='javascript:nicTemp()'}
            if (selected == '' && isChrome == false)
            {
                pasteHtmlAtCaret(this.inputs['title'].value || url,true);
            }
            if (!this.ln) {
                //var tmp = this.inputs['title'].value == "" ? this.inputs['href'].value : this.inputs['title'].value;
                this.ne.nicCommand("createlink", tmp);
                this.ln = this.findElm('A', 'href', tmp);
            }


    function getSelected()
    {
        if (document.selection)
            return document.selection.createRange().text;
        else
            return window.getSelection();
    }

    function pasteHtmlAtCaret(html) {
        var sel, range;
        if (window.getSelection) {
            // IE9 and non-IE
            sel = window.getSelection();
            if (sel.getRangeAt && sel.rangeCount) {
                range = sel.getRangeAt(0);
                range.deleteContents();

                // Range.createContextualFragment() would be useful here but is
                // non-standard and not supported in all browsers (IE9, for one)
                var el = document.createElement("div");
//create a link format
                el.innerHTML = '<a href="'+ html +'" title="" target="_blank">'+ html +'</a>';
                var frag = document.createDocumentFragment(), node, lastNode;
                while ( (node = el.firstChild) ) {
                    lastNode = frag.appendChild(node);
                }
                range.insertNode(frag);

                // Preserve the selection
                if (lastNode) {
                    range = range.cloneRange();
                    range.setStartAfter(lastNode);
                    range.collapse(true);
                    sel.removeAllRanges();
                    sel.addRange(range);
                }
            }
        } else if (document.selection && document.selection.type != "Control") {
            // IE < 9
            document.selection.createRange().pasteHTML(html);
        }
    }
于 2017-01-10T07:09:22.210 回答