简体   繁体   中英

Copy as html to clipboard programmatically

My problem is that user could get html copied to his clipboard without selecting it manually and Ctrl + C . The problem is that all plugins i tried (zClip, ZeroClipboard) copy just plain text. And then if i paste it into the Microsoft Word for example just text is pasted but not html which it represents. Is there any way to do that? I tried to find solution for my problem here and on the rest of the web but i couldnt.

window.zclip_copyTaskReference = function (event) {
    debugger;
    try {
        var self = $(event.target);
        var taskId = self.attr('data-task-id');
        var taskName = self.attr('data-task-name');
        var href = location.href.replace(/[#]$/, '');
        var link = $('<a></a>');
        link.attr('href', href);
        link.text(taskName);
        var html = link.wrap($('<div/>')).parent().html();
        successMessage('Link copied to clipboard');
        //return link[0];
        return html.trim();
        //return (html);
    } catch (e) {
        errorMessage('Failed to copy link');
        return '';
    }
};

init:

    $('.copy-link').zclip({
        path: '/Scripts/jquery/zclip/ZeroClipboard.swf',
        copy: window.zclip_copyTaskReference,
        afterCopy: $.noop()
    });

Try wrapping that code within either <pre> tags or <code> tags. If that doesn't work, you should be able to grab the content with jQuery's html method, serialize that into a string, and copy it to the clipboard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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