如何避免在使用更多读取脚本时消失布局



我想在我的网站的复制文本中添加更多阅读文本。因此,如果有人选择并复制文本并将其放置在剪贴板/其他来源,它将自动添加 阅读更多:来源的网址。

问题是我正在使用代码,但是当有人选择并复制文本时,文本的布局消失了。例如,白线和段落消失,所有文本将在一起而不会被白线或段落中断。

我过去尝试过几种解决方案,但大多数都像这段代码一样简单。

<script>
function addLink() {
 //Get the selected text and append the extra info
 var selection = window.getSelection(),
 pagelink = '<br /><br /> Lees meer op: ' + document.location.href + ' voor meer informatie', // Change this text
 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);
</script>

我想避免选择和复制的文本的布局消失。我该如何解决这个问题?提前谢谢。

奇怪的是,我终于在stackoverflow中找到了一个代码,它适用于我网站的phpbb部分,但不适用于我网站的WordPress部分。有谁知道我如何使这里提到的代码如何自动将文本附加到使用 JavaScript 复制的文本中,适用于我网站的 WordPress 部分?

最新更新