Javascript 创建一个按钮来输入文本,保存并复制粘贴到另一个平台上

Javascript 创建一个按钮来输入文本,保存并复制粘贴到另一个平台上,javascript,html,css,Javascript,Html,Css,我是一名聊天支持人员,熟悉HTML5/CSS3和一些JS。 我想要一个代码,在那里我可以在一个单独的按钮上输入一个脚本。 例如,“我能为您做些什么?”或“谢谢您联系我们” 该按钮存储并复制脚本,以便我可以在单击它时将其粘贴为回复。 这样,我可以根据查询的类别轻松地从多个按钮中进行选择。 我可以节省时间从愚蠢的重复性问题中输入冗长的回答 这是我所做的一个例子 <td colspan="1"><div id = "chat"> <button onclick="copy

我是一名聊天支持人员,熟悉HTML5/CSS3和一些JS。 我想要一个代码,在那里我可以在一个单独的按钮上输入一个脚本。 例如,“我能为您做些什么?”或“谢谢您联系我们” 该按钮存储并复制脚本,以便我可以在单击它时将其粘贴为回复。 这样,我可以根据查询的类别轻松地从多个按钮中进行选择。 我可以节省时间从愚蠢的重复性问题中输入冗长的回答

这是我所做的一个例子

<td colspan="1"><div id = "chat">
<button onclick="copy('Are you still there?')">Still there?</button>
<button onclick="copy('One moment please')">1moment</button></div>

还在吗?
1时刻

我也不知道,但是

您的复制函数如下所示。EXECCOMMAND('copy')函数从HTML文档复制“选择”。因此,首先我们创建一个spoof textarea元素,其中包含要复制的字符串。然后,我们执行命令将所选字符串复制到剪贴板。之后,欺骗元素被销毁。此函数还添加CSS属性,以确保用户的视觉效果不受影响

  const copy = str => {
      const el = document.createElement('textarea');  // Create a <textarea> element
      el.value = str;                                 // Set its value to the string that you want copied
      el.setAttribute('readonly', '');                // Make it readonly to be tamper-proof
      el.style.position = 'absolute';                 
      el.style.left = '-9999px';                      // Move outside the screen to make it invisible
      document.body.appendChild(el);                  // Append the <textarea> element to the HTML document
      const selected =            
        document.getSelection().rangeCount > 0        // Check if there is any content selected previously
          ? document.getSelection().getRangeAt(0)     // Store selection if found
          : false;                                    // Mark as false to know no selection existed before
      el.select();                                    // Select the <textarea> content
      document.execCommand('copy');                   // Copy - only works as a result of a user action (e.g. click events)
      document.body.removeChild(el);                  // Remove the <textarea> element
      if (selected) {                                 // If a selection existed before copying
        document.getSelection().removeAllRanges();    // Unselect everything on the HTML document
        document.getSelection().addRange(selected);   // Restore the original selection
      }
    };
const copy=str=>{
const el=document.createElement('textarea');//创建一个元素
el.value=str;//将其值设置为要复制的字符串
el.setAttribute('readonly','');//使其只读以防篡改
el.style.position='绝对';
el.style.left='-9999px';//移动到屏幕外部使其不可见
document.body.appendChild(el);//将元素附加到HTML文档中
所选常数=
document.getSelection().rangeCount>0//检查之前是否选择了任何内容
?document.getSelection().getRangeAt(0)//如果找到,则存储所选内容
:false;//标记为false以知道以前不存在选择
el.select();//选择内容
document.execCommand('copy');//copy-仅在用户操作(例如单击事件)的结果下工作
document.body.removeChild(el);//删除元素
if(selected){//如果复制前存在选择
document.getSelection().removeAllRanges();//取消选择HTML文档上的所有内容
document.getSelection().addRange(已选);//恢复原始选择
}
};