繁体   English   中英

如果我 select 并复制网站页面上的任何文本,它应该粘贴打开页面的 URL 如何使用 JavaScript

[英]If I select and copy any text on website page it should paste URL of the opened page how to do that using JavaScript

我希望用户 select 页面上的文本。 如果用户复制文本并尝试粘贴选定的文本,那么他应该只粘贴页面的 URL 而不是选定的文本。 我们如何使用 javascript 做到这一点?

这只是一个简单的演示如何截取剪贴板副本。 请记住,并非所有浏览器都支持此功能,但通常它可以工作:

 // Bind copy interceptor to the document document.addEventListener('copy', e => { // Set current page href to clipboard e.clipboardData.setData('text/plain', window.location.href); // Prevent default action e.preventDefault(); });
 html { font-size: 14px; }.box { width: 300px; border: 1px solid #000; padding: 5px; margin-bottom: 15px; resize: none; }
 <html> <div class="box"> There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain... </div> Paste to test: <div> <textarea class="box" rows="3"></textarea> </div> </html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM