我想添加一个阅读更多的文本到我的网站的拷贝文本。因此,如果有人选择并复制文本并将其放到剪贴板/其他源中,它将自动添加read more:源的url。
问题是我使用的是一个代码,但是当有人选择并复制一个文本时,文本的布局就消失了。例如,白线和段落消失了,所有的文本将不会被白线或段落打断。
我在过去尝试了几种解决方案,但大多数都像下面的代码一样简单。
<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>
我想避免选择和复制的文本的布局消失。我该如何解决这个问题呢?提前谢谢。
发布于 2019-01-11 05:12:24
奇怪的是,我终于在stackoverflow找到了一个代码,它可以与我网站的phpbb部分一起工作,但不能与我网站的WordPress部分一起工作。有谁知道我如何让这里提到的代码在我的网站的WordPress部分工作吗?
https://stackoverflow.com/questions/54133893
复制相似问题