[英]Javascript - Onclick() function only works once
我正在制作 function 以从 acess.php 文件中复制可编辑 div 的内容。 function 运行良好,但如果我编辑 div 并再次单击复制按钮,则在我重新启动页面之前它不再起作用。 有更多文本要复制,但出于隐私原因我减少了。
复制.js 文件:
document.querySelector("#foo").onclick = function() {
var divToCopy = document.querySelector("#selectPage");
var range = document.createRange();
range.selectNode(divToCopy);
window.getSelection().addRange(range);
document.execCommand("copy");
};
access.php 文件:
<head>
<button id="foo">Copy</button>
<meta charset="utf-8" />
<title>Acess</title>
<script src="../../js/jquery.js"></script>
<script src="../../js/copy.js"></script>
<link rel="stylesheet" href="../css/sit.css">
</head>
<body>
<div contenteditable="true" id="bodyEmail" style="border: solid 0.5px black; padding:1%; margin-top: 20px">
<div id="selectPage">
<main>
<h1 class='h1-principal'> Text to be copied <strong class='sub'><?= $func['SOLIC'] ?></strong></h1>
<p>Text to be copied <strong class='sub'><?= $name['NAME'] ?></strong> Text to be copied <strong class='sub'><?= $dateAdmin ?></strong></p>
</main>
</div>
</div>
在acess.php
文件中,您在head
标签中添加了按钮,它应该在body
标签中。
您可以使用selectAll
命令 select 内容
document.querySelector("#foo").onclick = function() { document.execCommand('selectAll',false,null); document.execCommand("copy"); };
<div contenteditable="true" id="bodyEmail" style="border: solid 0.5px black; padding:1%; margin-top: 20px"> <div id="selectPage"> <main> <h1 class='h1-principal'> Text to be copied <strong class='sub'><?= $func['SOLIC']?></strong></h1> <p>Text to be copied <strong class='sub'><?= $name['NAME']?></strong> Text to be copied <strong class='sub'><?= $dateAdmin?></strong></p> </main> </div> </div> <button id="foo">Copy</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.