将选定的TinyMCE编辑器内容复制到剪贴板

时间:2017-01-17 13:27:16

标签: jquery html tinymce

Jsfiddle示例:

http://jsfiddle.net/w0ap9Lun/1/

我的目标是选择TinyMCE textarea的所有内容并将其复制到剪贴板(相当于突出显示所有内容并按ctrl + c)。

我可以使用正常输入执行此操作:

$('.copyToclip').on('click', function() {
    //select the input
    $(this).siblings('input').select();
    //fire the copy command
    document.execCommand("copy");
    $(this).text('copied');
});

以下代码选择编辑器中的所有内容,但是当我调用' execCommand(" copy")'它没有复制到剪贴板,这是我的代码:

$('.copyTinyMCEToclip').on('click', function() {
    //select the content of the active tinyMCE instance 
    tinyMCE.activeEditor.selection.select(tinyMCE.activeEditor.getBody());
    document.execCommand("copy");
    $(this).text('copied');
});

非常感谢任何帮助。

5 个答案:

答案 0 :(得分:0)

你可以使用tinyMCE方法,试试这个:

jQuery(function(){
    jQuery('.copyTinyMCEToclip').click(function(){
      var selectedText = tinyMCE.activeEditor.selection.getContent();
      jQuery('input').attr('value', selectedText);
    });
});

示例http://jsfiddle.net/w0ap9Lun/2/

参考http://archive.tinymce.com/wiki.php/API3:method.tinymce.dom.Selection.getContent

答案 1 :(得分:0)

试试这个:

将文本复制到剪贴板的功能:

public static void mainjdk7(String ... args){

    Connection connect = Jsoup.connect("http://www.yahoo.com");
    try {
        Document dom = connect.get();
        dom.getElementsByTag("section").forEach(new Consumer<Element>() {
            @Override
            public void accept(Element element) {
                Elements imgstatus = element.getElementsByClass("imgstatus");
                if(null != imgstatus){
                    //Do something
                }

            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void mainjdk8(String ... args){
    Connection connect = Jsoup.connect("http://www.yahoo.com");
    try {
        Document dom = connect.get();
        dom.getElementsByTag("section").forEach(element -> {
            Elements imgstatus = element.getElementsByClass("imgstatus");
            if(null != imgstatus){
                //Do something
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
}

为控制分配值:

function copyToClipboard(text) {
if (window.clipboardData && window.clipboardData.setData) {
    // IE specific code path to prevent textarea being shown while dialog is visible.
    return clipboardData.setData("Text", text); 

} else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
    var textarea = document.createElement("textarea");
    textarea.textContent = text;
    textarea.style.position = "fixed";  // Prevent scrolling to bottom of page in MS Edge.
    document.body.appendChild(textarea);
    textarea.select();
    try {
        return document.execCommand("copy");  // Security exception may be thrown by some browsers.
    } catch (ex) {
        console.warn("Copy to clipboard failed.", ex);
        return false;
    } finally {
        document.body.removeChild(textarea);
    }
}}

答案 2 :(得分:0)

我通过优化搜索查询找到了解决方案。问题是更广泛地将HTML添加到剪贴板,我在这里找到答案:

Javascript - Copy string to clipboard as text/html

答案 3 :(得分:0)

让jquery单击tinymce菜单图标。唯一的缺点是,编辑器将保留所有选定的内容。

$(".mce-i-selectall").click();
$(".mce-i-copy").click();

答案 4 :(得分:0)

通过tinyMCE API而非文档对象或JQuery调用“ copy”命令的最简单解决方案。

tinyMCE.activeEditor.selection.select(tinyMCE.activeEditor.getBody());
tinyMCE.activeEditor.execCommand( "Copy" );