我想包括一个现有网页上的按钮,将文本复制到Windows剪贴板。
网页和其中的PHP已经可以很好地创建和显示如下文本:
网页上的输出:
'Abby Normal' <abnormal@rockyhorror.com>, 'Brad Majors' <bm@rockyhorror.com>, 'Frank N. Furter' <franknfurter@rockyhorror.com>
因此,现在我想添加一个Javascript函数和一个html按钮,该按钮调用该函数将输出复制到Windows剪贴板。
问题:按下按钮时不会复制任何内容。我做错了什么?提前谢谢你。
<?PHP
session_start();
include('include/_initsess.php');
include('include/_setdb.php');
if(!isset($_SESSION['userlist'])) exit;
$users = $_SESSION['userlist'];
$emails = '';
$qry = "SELECT FIRST,LAST,EMAIL FROM users WHERE PKEY IN ($users)";
$result = mysql_query($qry);
$numrows = mysql_num_rows($result);
for ($m=0; $m<$numrows; $m++) {
$row = mysql_fetch_array($result);
list($fn,$ln,$em) = $row;
$emails .= ($m==0) ? "'".$fn." ".$ln."' <".$em.">" : (", '".$fn." ".$ln."' <".$em.">");
} // end for
?>
<html>
<head>
</head>
<body>
<span class=mono id="theList" value="<?php echo $emails; ?>">
<?PHP echo($emails); ?>
</span>
<script>
function copyToClipboardWithJavascript() {
/* Get the text field */
var copyText = document.getElementById("theList");
/* Select the text field */
copyText.select();
/* Copy the text inside the text field */
document.execCommand("copy");
}
</script>
<button onclick="copyToClipboardWithJavascript()">Click here</button>
</span>
</body>
</html>
我尝试过Javascript教程建议的方法:
var copyText = = document.getElementById("theList");
以及在Javascript中使用PHP的我自己的变体:
var copyText = <?PHP echo($emails); ?>;
var copyText = `<?PHP echo($emails); ?>`;
var copyText = "<?PHP echo($emails); ?>";
var copyText = '<?PHP echo($emails); ?>';
但结果是nothing不会导致任何错误,也不会将任何内容复制到剪贴板。
我知道网页会立即被保存和使用,因为我还对按钮中的字母“单击此处”进行了微小的更改,并且在刷新后可以看到不同之处。enter code here
*UPDATE WITH ANSWER我使用:*
<span class=mono id="theList">
<?PHP echo($emails); ?>
</span>
<button id="copyButton" onclick="myCopyFunction()">Copy email address list to clipboard.</button>
<script>
function myCopyFunction() {
var myText = document.createElement("textarea")
myText.value = document.getElementById("theList").innerHTML;
myText.value = myText.value.replace(/</g,"<");
myText.value = myText.value.replace(/>/g,">");
document.body.appendChild(myText)
myText.focus();
myText.select();
document.execCommand('copy');
document.body.removeChild(myText);
}
</script>
发布于 2018-06-07 05:18:56
不能直接从字符串复制,只能从HTML元素复制。您需要将PHP字符串放入元素的值中。
function copyToClipboardWithJavascript() {
/* Get the text field */
var copyText = document.getElementById("theList");
/* Put emails into the text field */
copyText.value = <?php echo json_encode($emails); ?>;
/* Select the text field */
copyText.select();
/* Copy the text inside the text field */
document.execCommand("copy");
}
发布于 2021-12-02 13:02:36
这个简单的解决方案可能对你有用。这就是我所使用的。和jQuery在一起。
function copy() {
navigator.clipboard.writeText($('#link-to-copy').val());
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span onclick="copy();">
Copy link
</span>
<input type="hidden" id="link-to-copy" value="<?= $link_copy ?>">
当您单击"Copy link“span元素时,它将调用copy()函数,然后将隐藏输入的值( id为”link- to -copy“)写入到导航器的剪贴板中。
隐藏输入的值可以是您想要的任何值,这里我使用的是PHP变量。这个PHP值将被复制到你的剪贴板上。
https://stackoverflow.com/questions/50729670
复制相似问题