复制到剪贴板无法粘贴

时间:2015-02-11 05:50:25

标签: javascript html5 clipboard clipboarddata

我尝试了以下代码并运行它。看来我可以成功创建ClipboardEvent来复制信息。但是,我无法在MS Word中使用Ctrl + V粘贴它。

浏览器:Firefox(v35.0.1)

            // behavior to copy elements with format
            $("#copy-formatted").click(function(e){
                var text = $("#container").html();
                console.log(text);

                // first attempt => data is successfully saved to copyEvent
                /*var copyEvent = new ClipboardEvent('copy', { dataType: 'text/plain', data: text } );
                console.log(copyEvent);
                document.dispatchEvent(copyEvent);*/

                // ClipboardEvent is not recognized by IE11
                var clip = new ClipboardEvent('copy');
                clip.clipboardData.setData('text/plain', "test");
                clip.preventDefault();
                clip.returnValue = false;

                // *********************************** //
                // data is successfully retrieved here //
                // *********************************** //
                console.log(clip.clipboardData.getData('text/plain'));

                // *** > dispatching not sure how this works. seems not working.
                //e.target.dispatchEvent(clip);
                document.dispatchEvent(clip);


                console.log("finished dispatching event");
            });

请帮忙。

0 个答案:

没有答案