首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >用PHP和JavaScript将文本复制到剪贴板?

用PHP和JavaScript将文本复制到剪贴板?
EN

Stack Overflow用户
提问于 2018-06-07 05:10:50
回答 2查看 24.5K关注 0票数 5

我想包括一个现有网页上的按钮,将文本复制到Windows剪贴板。

网页和其中的PHP已经可以很好地创建和显示如下文本:

网页上的输出:

代码语言:javascript
运行
复制
'Abby Normal' <abnormal@rockyhorror.com>, 'Brad Majors' <bm@rockyhorror.com>, 'Frank N. Furter' <franknfurter@rockyhorror.com>

因此,现在我想添加一个Javascript函数和一个html按钮,该按钮调用该函数将输出复制到Windows剪贴板。

问题:按下按钮时不会复制任何内容。我做错了什么?提前谢谢你。

代码语言:javascript
运行
复制
<?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."' &lt;".$em."&gt;" : (", '".$fn." ".$ln."' &lt;".$em."&gt;");
    } // 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教程建议的方法:

代码语言:javascript
运行
复制
var copyText = = document.getElementById("theList");

以及在Javascript中使用PHP的我自己的变体:

代码语言:javascript
运行
复制
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我使用:*

代码语言:javascript
运行
复制
<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(/&lt;/g,"<");
  myText.value = myText.value.replace(/&gt;/g,">");
  document.body.appendChild(myText)
  myText.focus();
  myText.select();
  document.execCommand('copy');
  document.body.removeChild(myText);
}
</script>
EN

回答 2

Stack Overflow用户

发布于 2018-06-07 05:18:56

不能直接从字符串复制,只能从HTML元素复制。您需要将PHP字符串放入元素的值中。

代码语言:javascript
运行
复制
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");
}
票数 3
EN

Stack Overflow用户

发布于 2021-12-02 13:02:36

这个简单的解决方案可能对你有用。这就是我所使用的。和jQuery在一起。

代码语言:javascript
运行
复制
function copy() {
  navigator.clipboard.writeText($('#link-to-copy').val());
}
代码语言:javascript
运行
复制
<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值将被复制到你的剪贴板上。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50729670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档