我正在尝试编写JS代码,它可以复制td的内容,同时显示某种通知,让用户知道某些内容已复制到剪贴板。我尝试了以下代码,复制到剪贴板工作正常,但通知从来没有工作。我该怎么办?
PHP和HTML:
echo "<td onClick='copy(this),selectThis(this)'> " . $row['email'] . "</td>";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>发布于 2020-08-13 23:09:40
'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>
https://stackoverflow.com/questions/63387886
复制相似问题