0

我正在使用此代码向我复制的文本添加阅读更多链接,但忽略换行符和格式

 <script type='text/javascript'>
function addLink() {
    var body_element = document.getElementsByTagName(&#39;body&#39;)[0];
    var selection;
    selection = window.getSelection();
  var pagelink = &quot;<br/><br/> Mai multe Bancuri pe: http://bancuricubarbatisifemei.blogspot.com/ <br/>&quot;; // change this if you want
    var copytext = selection + pagelink;
    var newdiv = document.createElement(&#39;div&#39;);
    newdiv.style.position=&#39;absolute&#39;;
    newdiv.style.left=&#39;-99999px&#39;;
    body_element.appendChild(newdiv);
    newdiv.innerHTML = copytext;
    selection.selectAllChildren(newdiv);
    window.setTimeout(function() {
        body_element.removeChild(newdiv);
    },0);
}
document.oncopy = addLink;
</script>

如何保存它们?

4

2 回答 2

2

所选内容被复制为纯文本,为了保留换行符和格式,您必须将所选文本获取为 HTML。

JSFiddle demo here

JavaScript 代码:

function addLink() {
    var selection = window.getSelection();

    var htmlDiv = document.createElement("div");
    for (var i = 0; i < selection.rangeCount; ++i) {
        htmlDiv.appendChild(selection.getRangeAt(i).cloneContents());
    }
    var selectionHTML = htmlDiv.innerHTML;

    var pagelink = "<br/><br/>Read more: http://www.stackoverflow.com/ <br/>";
    var copytext = selectionHTML + pagelink;

    var newdiv = document.createElement('div');
    newdiv.style.position = 'absolute';
    newdiv.style.left = '-99999px';

    document.body.appendChild(newdiv);
    newdiv.innerHTML = copytext;
    selection.selectAllChildren(newdiv);
    window.setTimeout(function () { document.body.removeChild(newdiv); }, 0);
}
document.oncopy = addLink;
于 2013-06-18T18:17:00.707 回答
0

您不知何故错过了一些报价,相信thisthis是它的来源。

在进行以下更正(工作演示)后,猜猜它可以按您的意图工作:

function addLink() {
        var body_element = document.getElementsByTagName("body")[0];
        var selection = window.getSelection();
        var pagelink = '< br/> < br/> Mai multe Bancuri pe: http://bancuricubarbatisifemei.blogspot.com/ <br/>'; 
        var copytext = selection + pagelink;
        var newdiv = document.createElement('div');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        body_element.appendChild(newdiv);
        newdiv.innerHTML = copytext;
        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {
            body_element.removeChild(newdiv);
        }, 0);
    }

document.oncopy = addLink;

编辑:

尝试使用pre而不是div& 换行符 (' \n ') 来保留换行符。(更新演示

function addLink() {
        var body_element = document.getElementsByTagName("body")[0];
        var selection = window.getSelection();
        var pagelink = '\n\nMai multe Bancuri pe: http://bancuricubarbatisifemei.blogspot.com/\n'; 
        var copytext = selection + pagelink;
        var newdiv = document.createElement('pre');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        body_element.appendChild(newdiv);
        newdiv.innerHTML = copytext;
        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {
            body_element.removeChild(newdiv);
        }, 0);
    }

document.oncopy = addLink;
于 2013-06-18T18:09:39.767 回答