当前位置:网站首页>Native JS implements the copy text function
Native JS implements the copy text function
2022-06-12 12:32:00 【I'm Mr.C】
Native JS Realize the function of copying text
Native JS Implementation of replication .
It uses DOM Of execCommand.
<input type="text">
<button onClick="copyFn()"> Click replication </button>
<script>
function copyFn(){
let txt=document.getElementsByTagName('input')[0].value;
copy(txt);
}
function copy(item){
let span=document.createElement('span');
let Text=document.createTextNode(item);
span.appendChild(Text);
document.body.appendChild(span);
window.getSelection().selectAllChildren(span);
document.execCommand ("Copy");
document.body.removeChild(span);
}
</script>
establish html Elements , because execCommand You must copy the label elements that exist on the page , Wait until the text is copied to the clipboard , Remove element again .

边栏推荐
- Left and right cases + rotating pictures of small dots + no time
- 关系代数笛卡尔积和自然连接的例子
- Visio 2019 uses PJ
- [transfer]placement NEW
- Promise knowledge
- itk::Neighborhood获取6邻域、18邻域、26邻域,18/6邻域,26/18邻域
- Advanced C language -- storage of floating point in memory
- 屏蔽不显示VS警告warning
- JS attribute operation and node operation
- itk neighbhood
猜你喜欢

关系代数笛卡尔积和自然连接的例子

JS convert string to array object

Promise understanding has used promise to realize picture preloading (sequential loading)

Reasons for college students' leave
![[JS] some handwriting functions: deep copy, bind, debounce, etc](/img/f8/cf51a24450a88abb9e68c78e0e3aa8.jpg)
[JS] some handwriting functions: deep copy, bind, debounce, etc

NewOJ Week 10题解

Rust语言学习

C语言深度解剖篇——关键字&&补充内容

JS built in object

Problems encountered in installing canvas and errors encountered in running the project
随机推荐
Map and set of ES6
JS convert string to array object
AutoLock 解决加锁后忘记解锁问题
object. Defineproperty basic usage
Open source project - (erp+ Hotel + e-commerce) background management system
轻量化---Project
sublime_ Textuse
C语言进阶篇——浮点型在内存中的存储
22年gdcpc广东省赛记录
Stress - system pressure simulation tool
JS how to get the values of multiple objects in an array
vtk 图像序列鼠标交互翻页
imx6-uboot添加lvds1显示
Click to generate 4-bit random number and verify code setting
深度剖析指针的进阶——C语言的进阶篇
Suggestions and skills for advanced version of go language test
Deep analysis of advanced pointer -- advanced chapter of C language
点云配准--gicp原理与其在pcl中的使用
Difference between Definition and Declaration
获取本机所有ipv4, ipv6地址