简体   繁体   English

document.execCommand('unlink')不适用于Firefox中的锚点

[英]document.execCommand('unlink') doesn't work for anchors in Firefox

I'm using document.execCommand("unlink", false, false); 我正在使用document.execCommand("unlink", false, false); to remove hyperlinks and anchors in my own html editor. 删除我自己的HTML编辑器中的超链接和锚点。 It works fine except in one case: it doesn't remove anchors in Firefox. 它工作正常,除了一种情况:它不会删除Firefox中的锚点。 Is this not supported in Firefox or is there another way to solve this? 这是不是在Firefox中支持,还是有其他方法可以解决这个问题?

Here's my example: 这是我的例子:

<div contenteditable="true">This is an <a name="anker">anchor</a>. And this is a <a href="#">normal hyperlink</a>. Please try to select them and then press the "Unlink" button.</div>

<input type="button" id="btn" value="Unlink">

$(document).ready(function() {    
    $('#btn').click(function() {
        document.execCommand("unlink", false, false);
    });
});

Check it out on Fiddle 在小提琴上看看吧

To answer your question about what alternatives there are, you could force the issue and set any a element's href attribute before calling unlink : 要回答你什么替代品有问题,你可以强制的问题,并设置任何a元素的href之前调用属性unlink

$(document).ready(function() {    
    var $editable = $('[contenteditable]');

    $('#btn').click(function() {
        $editable.find('a:not([href])').attr('href', '#');
        document.execCommand("unlink", false, false);
    });
});

http://jsfiddle.net/Bz7pR/7/ http://jsfiddle.net/Bz7pR/7/

There are, of course, probably multiple ways to "solve" the problem. 当然,可能有多种方法可以“解决”这个问题。 I figure $.unwrap() would probably work as well. 我想$.unwrap()可能也会起作用。 I'm not that versed on document.execCommand and rich text editors, but I imagine you need to be careful, precise, and test test test. 我不是很熟悉document.execCommand和富文本编辑器,但我想你需要小心,精确和测试测试。

Note, that is just a demonstration; 注意,这只是一个示范; you've really got to think about that and consider exactly how you're going to handle that problem. 你真的要考虑这个问题并考虑你将如何处理这个问题。 For instance, you could detect and only run that if Firefox is the browser, which would limit any unexpected damage or outcomes you might have. 例如,如果Firefox是浏览器,您可以检测并仅运行它,这将限制您可能遇到的任何意外损坏或结果。

EDIT 编辑

And here is a more complete version that only affects the selected text range: 这是一个更完整的版本,只影响所选的文本范围:

$(document).ready(function() {    
    $('#btn').click(function unlink() {
        var holder,
            frag,
            range,
            child;

        if (window.getSelection && window.getSelection().getRangeAt) {
            holder = document.createElement('div');
            frag = document.createDocumentFragment();
            range = window.getSelection().getRangeAt(0);

            $(holder)
                .append(range.cloneContents())
                .find('a:not([href])')
                .attr('href', '#');

            while ((child = holder.firstChild)) {
                frag.appendChild(child);
            }

            range.deleteContents();
            range.insertNode(frag);
        }

        document.execCommand("unlink", false, false);
    });    
});

http://jsfiddle.net/Bz7pR/12/ http://jsfiddle.net/Bz7pR/12/

I'm no genius when it comes to text ranges and selections, so I modified the code found in this answer: 在文本范围和选择方面,我不是天才,所以我修改了这个答案中的代码:

https://stackoverflow.com/a/6252893/451969 https://stackoverflow.com/a/6252893/451969

To come up with what is above. 想出上面的内容。 If someone sees something that's either wrong or incoherent, leave me a comment below. 如果有人看到某些错误或不连贯的内容,请在下面留言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM