简体   繁体   English

Javascript - 如何在不显示文本区域的情况下将文本复制到剪贴板?

[英]Javascript - How can I copy a text to clipboard without displaying a textarea?

I'm trying to copy to the user's clipboard a path to the log files in Javascript.我正在尝试将 Javascript 中日志文件的路径复制到用户的剪贴板。 However, I do not wish to use an input field or textarea.但是,我不希望使用输入字段或文本区域。 Other solutions indicate that I should use an input field, select the text from the field, then copy it.其他解决方案表明我应该使用输入字段,从字段中选择文本,然后复制它。 Is there any workaround to have a code-defined string directly copied to my clipboard?是否有任何解决方法可以将代码定义的字符串直接复制到我的剪贴板? Thanks.谢谢。

  • I use plain JS and Node.js我使用纯 JS 和 Node.js
  • I am trying to copy a text defined in my code (local path to log files) to the user's clipboard without using any user input fields (textarea, input tags).我正在尝试将我的代码中定义的文本(日志文件的本地路径)复制到用户的剪贴板,而不使用任何用户输入字段(文本区域、输入标签)。 The other questions seem to either use user fields or 3rd party APIs.其他问题似乎要么使用用户字段,要么使用 3rd 方 API。 I do not want that!我不要那个! I am looking for some JS code that will accomplish all this.我正在寻找一些可以完成所有这些的 JS 代码。

As you have already read from other post, you need the textarea or textbox to copy to clipboard, because of browser security, If you don't want to show the textbox or textarea, just hide it, you can use正如您已经从其他帖子中阅读的那样,由于浏览器安全性,您需要将 textarea 或 textbox 复制到剪贴板,如果您不想显示 textbox 或 textarea,只需将其隐藏,您可以使用

.hide {
  position:absolute;
  left:-9999px
}

left: value should be greater either positive or negative, ie it does not fall into any current screen visible width. left:值应该大于正值或负值,即它不属于任何当前屏幕可见宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM