为了账号安全,请及时绑定邮箱和手机立即绑定

使用 javascript 将 PHP 变量复制到剪贴板

使用 javascript 将 PHP 变量复制到剪贴板

PHP
POPMUISE 2024-01-19 10:00:05
我正在尝试foreach使用 . 将使用循环生成的值复制到剪贴板JS。无论单击哪一行,都只会复制第一行。以下是生成要复制的值的代码:foreach($results_array as $value) {    /*Some PHP code*/     <input class="col-sm-10" title="Copy Link" type="text" id="copy_this" value='<?php echo  $user_folder."/".$value; ?>' onclick="copyTo()"/>}我的JS函数:function copyTo() {  /* Get the text field */  var copyText = document.getElementById("copy_this");                                  /* Copy the text inside the text field */  document.execCommand("copy");}
查看完整描述

1 回答

?
qq_笑_17

TA贡献1818条经验 获得超7个赞

ID 的全部目的是唯一地标识一个元素。另外,这不是execCommand()工作原理(您的代码怎么可能知道您想要复制的文本?)。


摆脱 ID(你根本不需要),你可以这样做:


function copyTo(input) {

  input.select();

  document.execCommand("copy");

}

<input type="text" value="First" onclick="copyTo(this)">

<input type="text" value="Second" onclick="copyTo(this)">

<input type="text" value="Third" onclick="copyTo(this)">


查看完整回答
反对 回复 2024-01-19
  • 1 回答
  • 0 关注
  • 23 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信