Javascript 如何将额外信息添加到复制的web文本[开始和结束]

Javascript 如何将额外信息添加到复制的web文本[开始和结束],javascript,html,Javascript,Html,下面的javascript用于在复制的文本末尾附加链接。我想在文本的开头添加链接,而不是在底部。我怎么做 编辑:得到了以上问题的答案,现在我想知道如何在开头和结尾同时添加它。还想知道如何在复制文本的中间添加它。< /P> function addLink() { //Get the selected text and append the extra info var selection = window.getSelection(), p

下面的javascript用于在复制的文本末尾附加链接。我想在文本的开头添加链接,而不是在底部。我怎么做

编辑:得到了以上问题的答案,现在我想知道如何在开头和结尾同时添加它。还想知道如何在复制文本的中间添加它。< /P>
function addLink() {
        //Get the selected text and append the extra info
        var selection = window.getSelection(),
            pagelink = '<br /><br /> Read more at: ' + document.location.href,
            copytext = selection + pagelink,
            newdiv = document.createElement('div');

        //hide the newly created container
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';

        //insert the container, fill it with the extended text, and define the new selection
        document.body.appendChild(newdiv);
        newdiv.innerHTML = copytext;
        selection.selectAllChildren(newdiv);

        window.setTimeout(function () {
            document.body.removeChild(newdiv);
        }, 100);
    }

    document.addEventListener('copy', addLink);
函数addLink(){
//获取所选文本并附加额外信息
var selection=window.getSelection(),
pagelink='

阅读更多信息,请访问:'+document.location.href, copytext=选择+页面链接, newdiv=document.createElement('div'); //隐藏新创建的容器 newdiv.style.position='absolute'; newdiv.style.left='-9999px'; //插入容器,用扩展文本填充,然后定义新选择 文件.body.appendChild(newdiv); newdiv.innerHTML=copytext; 选择。选择所有子项(newdiv); setTimeout(函数(){ 文件.body.removeChild(newdiv); }, 100); } 文件。附录列表(“副本”,附录链接);
Um。。你试过倒序吗?像
copytext=pagelink+selection
?我没有。我现在就试试,谢谢。它起作用了。顺便问一下,你是否知道如何在复制文本的中间添加它?编辑问题。我想知道如何在开始和结束时同时添加,也在中间。