3

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

浏览器:火狐(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");
            });

请帮忙。

4

0 回答 0