0

我正在尝试编写一个添加链接的函数,以便我的用户可以选择文本并将链接附加到文本。(我试图模仿 gmail 如何轻松地让我们添加、删除或编辑指向电子邮件中选定文本的链接)

这是我在 Google'ng 之后提出的解决方案的代码。

var sel, range;
    if (window.getSelection) {
        sel = window.getSelection();
        if (sel.rangeCount) {
            range = sel.getRangeAt(0);
            var temp = range;
            var link =prompt("Please enter the link for this selection \n "+range+"","");
               range.insertNode(document.createTextNode("<a href='"+link+"' target='_blank'>"+temp+"</a>"));
        }
    } else if (document.selection && document.selection.createRange) {
        range = document.selection.createRange();
        var link =prompt("Please enter the link for this selection \n "+range.text+"","");
        range.text = "<a href='"+link+"' target='_blank' onClick='changeLink()'>"+range.text+"</a>";
    }

代码不像我想象的那样工作,链接被添加到内容中,但它<a>也在内容内部显示标签,而不是显示带有下划线的默认蓝色文本。如何隐藏<a>标签?还有我将如何允许他们删除或编辑链接。

(更新)解决方案:

function addFormat(type) {
    var savedSel = saveSelection();
    if(savedSel != '') {
        var link =prompt("Please enter the link for this selection \n "+savedSel+"","");
        restoreSelection(savedSel);
        document.execCommand("CreateLink", false, link);
        var links = getLinksInSelection();
        for (var i = 0; i < links.length; ++i) {
            links[i].setAttribute('target','_blank');
        }
    } else { alert("Please select some text to insert the link"); }
}

 function getLinksInSelection() {
        var selectedLinks = [];
        var range, containerEl, links, linkRange;
        if (window.getSelection) {
            sel = window.getSelection();
            if (sel.getRangeAt && sel.rangeCount) {
                linkRange = document.createRange();
                for (var r = 0; r < sel.rangeCount; ++r) {
                    range = sel.getRangeAt(r);
                    containerEl = range.commonAncestorContainer;
                    if (containerEl.nodeType != 1) {
                        containerEl = containerEl.parentNode;
                    }
                    if (containerEl.nodeName.toLowerCase() == "a") {
                        selectedLinks.push(containerEl);
                    } else {
                        links = containerEl.getElementsByTagName("a");
                        for (var i = 0; i < links.length; ++i) {
                            linkRange.selectNodeContents(links[i]);
                            if (linkRange.compareBoundaryPoints(range.END_TO_START, range) < 1 && linkRange.compareBoundaryPoints(range.START_TO_END, range) > -1) {
                                selectedLinks.push(links[i]);
                            }
                        }
                    }
                }
                linkRange.detach();
            }
        } else if (document.selection && document.selection.type != "Control") {
            range = document.selection.createRange();
            containerEl = range.parentElement();
            if (containerEl.nodeName.toLowerCase() == "a") {
                selectedLinks.push(containerEl);
            } else {
                links = containerEl.getElementsByTagName("a");
                linkRange = document.body.createTextRange();
                for (var i = 0; i < links.length; ++i) {
                    linkRange.moveToElementText(links[i]);
                    if (linkRange.compareEndPoints("StartToEnd", range) > -1 && linkRange.compareEndPoints("EndToStart", range) < 1) {
                        selectedLinks.push(links[i]);
                    } 
                }
            }
        }
        return selectedLinks;
    }

function saveSelection() {
    if (window.getSelection) {
        sel = window.getSelection();
        if (sel.getRangeAt && sel.rangeCount) {
            var ranges = [];
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                ranges.push(sel.getRangeAt(i));
            }
            return ranges;
        }
    } else if (document.selection && document.selection.createRange) {
        return document.selection.createRange();
    }
    return null;
}

function restoreSelection(savedSel) {
    if (savedSel) {
        if (window.getSelection) {
            sel = window.getSelection();
            sel.removeAllRanges();
            for (var i = 0, len = savedSel.length; i < len; ++i) {
                sel.addRange(savedSel[i]);
            }
        } else if (document.selection && savedSel.select) {
            savedSel.select();
        }
    }
}

谢谢。

4

0 回答 0