javascript - copy an element as HTML to clipboard - Stack Overflow

I've managed to write some jQuery to find an element and copy it's html to the clipboard (ie

I've managed to write some jQuery to find an element and copy it's html to the clipboard (ie only).

The problem is that when I paste this into a rich text box area in sharepoint it pastes the HTML as text only.

How do i replicate the user action of highlighting a link on a page and pressing copy. When I do this manually and then paste the clipboard contents the rich text area realises that it is markup and replicates the link as an anchor in the text content.

I've managed to write some jQuery to find an element and copy it's html to the clipboard (ie only).

The problem is that when I paste this into a rich text box area in sharepoint it pastes the HTML as text only.

How do i replicate the user action of highlighting a link on a page and pressing copy. When I do this manually and then paste the clipboard contents the rich text area realises that it is markup and replicates the link as an anchor in the text content.

Share asked May 26, 2010 at 12:10 Brian ScottBrian Scott 9,3717 gold badges48 silver badges68 bronze badges 1
  • Is there definetely no alternative way to do this then? – Brian Scott Commented May 26, 2010 at 13:21
Add a ment  | 

2 Answers 2

Reset to default 2

Unfortunately, as far as I know the only programmatic access IE gives to the clipboard allows you to set text data and URL data, but nothing else: http://msdn.microsoft./en-us/library/ms536744(v=VS.85).aspx

This works:

window.clipboardData.setData("text", "<div>Testing</div>");

...but has the problem you mentioned. Sadly, this doesn't work:

window.clipboardData.setData("html", "<div>Testing</div>");

A bit surprising, really.

This is what I used to copy/paste a hyperlink HTML element to the clipboard so that when you paste it the href is hidden and you only see the "prettier" name.

HTML:

<button onclick="copyToClipboard()">Copy me!</button>

Javascript:

var copyToClipboard = (function() {
    var _dataString = null;
    $('.transform').toggleClass('transform-active');
    document.addEventListener("copy", function(e){
        if (_dataString !== null) {
            try {
                e.clipboardData.setData("text/html", link);
                e.preventDefault();
            } finally {
                _dataString = null;
            }
        }
    });
    return function(data) {
        _dataString = data;
        document.execCommand("copy");
    };
})();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744911669a4600589.html

相关推荐

  • javascript - copy an element as HTML to clipboard - Stack Overflow

    I've managed to write some jQuery to find an element and copy it's html to the clipboard (ie

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信