本文介绍了添加目标="_ blank"与execCommand'createlink'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为自定义CMS创建一个微型WYSIWYG编辑器.它具有添加和删除链接的选项.它可以很好地添加链接,但是希望可以选择将target="_blank"添加到超链接.另外,如果可能的话,我希望能够添加alt=""title="".

I am attempting to create a mini WYSIWYG editor for a custom CMS. It has the option to add and remove links. It adds links fine, but would like to have the option to add target="_blank" to the hyperlink. Also, if possible, I would like to be able to add alt="" and title="".

目前,这是我的代码:

function addLink() {
    var linkURL = prompt('Enter a URL:', 'http://');
    editorWindow.document.execCommand('createlink', false, linkURL);
}

曾经四处张望,似乎找不到解决方法.我见过的大多数解决方案都说要添加:

Been looking around, and can't seem to find a solution. Most of the solutions I've seen say to add:

function addLink() {
    var linkURL = prompt('Enter a URL:', 'http://');
    var newLink = editorWindow.document.execCommand('createlink', false, linkURL);
    newLink.target = "_blank";
}

但这似乎不起作用.有什么建议吗?

But this doesn't seem to work. Any suggestions?

推荐答案

我能够找到解决方案.不知道这是否是正确的方法,但是它有效.在 https://stackoverflow.com/a/5605841/997632 之后,这就是我用于代码工作的内容:

I was able to find a solution. Don't know if this is the right way to go, but it works. Following https://stackoverflow.com/a/5605841/997632, this is what I used for my code to work:

function addLink() {
    var linkURL = prompt('Enter a URL:', 'http://');
    var sText = editorWindow.document.getSelection();

    editorWindow.document.execCommand('insertHTML', false, '<a href="' + linkURL + '" target="_blank">' + sText + '</a>');
}

以防万一其他人正在寻找并偶然发现...

Just in case anyone else is looking and stumbles upon this...

这篇关于添加目标="_ blank"与execCommand'createlink'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:56