简体   繁体   English

复制 JavaScript 中的元素内容

[英]Copy <td> element content in JavaScript

I'm trying to write JS code that can copy the content of a td and, at the same time, shows some kind of notification to let the user know that something has been copied to the clipboard.我正在尝试编写可以复制td内容的 JS 代码,同时显示某种通知,让用户知道某些内容已复制到剪贴板。 I tried the following code and the copying to clipboard works fine, but the notification never worked.我尝试了以下代码并且复制到剪贴板工作正常,但通知从未奏效。 What should I do?我应该怎么办?

PHP and HTML: PHP 和 HTML:

echo "<td onClick='copy(this),selectThis(this)'> " . $row['email'] . "</td>";

JS: JS:

<script type="text/javascript">
    function copy(that){
        var inp =document.createElement('input');
        document.body.appendChild(inp)
        inp.value =that.textContent
        inp.select();
        document.execCommand('copy',false);
        inp.remove();
    }
    function selectThis(element) {
        document.createElement("input")).style.backgroundColor = "red";
    }
</script>

 'use strict'; function copy(that){ //... notify(that.textContent); } function notify(content) { let notification = document.createElement('span'); notification.classList.add('notification'); notification.textContent = `"${content}" copied`; document.body.prepend(notification); setTimeout(() => notification.remove(), 4000); }
 .notification { border: solid 1px green; padding: 5px; animation-name: fade; animation-duration: 4s; animation-iteration-count: 2; animation-direction: alternate; } @keyframes fade { from { opacity:0; } to { opacity:1; } }
 <p onclick="copy(this)" id="element" >Click here to copy this text (figuratively).</p>

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

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