当前位置:网站首页>JS realizes the function of one click Copy
JS realizes the function of one click Copy
2022-07-29 03:57:00 【1 Jiajia Jiajia】
Reference resources WEB API Interface
https://developer.mozilla.org/zh-CN/docs/Web/API/Document/execCommand
Realization
document.execCommand(‘copy’)
grammar
bool = document.execCommand(aCommandName, aShowDefaultUI, aValueArgument)
Parameters
aCommandName: Command name ,copy,cut etc.
aShowDefaultUI: One Boolean, Whether to display the user interface , It's usually false.
aValueArgument: Some orders ( for example insertImage) Additional parameters are required (insertImage Need to provide insert image Of url), The default is null, Generally do not use .
If you want to copy Content , Only editable areas (input,textarea) To operate , Therefore, the implementation needs to be divided into replication input Content and replication are not input Content .
- Copy input Content : Code to react+antd demo For example
// antd Of input Components ,class by ant-input
<Input style={
{maxWidth: '270px', marginRight: '10px'}} onChange={(e)=>this.changeInput(e)} value={this.state.inputValue} />
<Button type="primary" onClick={()=>this.fzValue()}> Click one button to copy the content </Button>
fzValue() {
console.log(' Click the one click Copy content button ')
if (document.execCommand('copy')) {
const input = document.getElementsByClassName('ant-input')
input[0].select()
document.execCommand('copy')
alert(' Replication success ')
}
}
- Copy non input Content , Need to create input box ,value For what you want to copy
<div className={'hello-div'}>Hello</div>
<Button type="primary" onClick={()=>this.fzValue()}> increase </Button>
fzValue() {
if (document.execCommand('copy')) {
// Get content to copy
const value = document.getElementsByClassName('hello-div')[0].innerHTML
// establish input
const input = document.createElement('input')
// take input Of value Set to the content to be copied
input.setAttribute('value', value);
document.body.appendChild(input);
input.select()
document.execCommand('copy')
// The destruction input
input.remove()
}
}
边栏推荐
- Why do programmers so "dislike" the trunk development mode?
- Code ~ hide or disable the status bar and virtual keys
- The data type of symbol, a new feature of ES6
- Let variable declaration feature of ES6 new feature and its case
- UCOS task switching process
- Simple use of eventbus
- 从2019 年开始,你一定停止使用了这个营销策略…
- 《陌路曾相逢》夏陌沈疏晏_夏陌沈疏晏最新章节
- 1. 头文件-注释-命名空间-标准输入输出流
- Connect with third-party QQ login
猜你喜欢
Ribbon principle analysis namedcontextfactory
RHCE's at, crontab's basic operations, the Chrony service, symmetric encryption and asymmetric encryption
SQL窗口函数
Typescript from getting started to mastering (XVI) configuration file - first knowledge of compileroptions configuration item
Ssl== certificate related concepts
Interview essential! TCP classic 15 consecutive questions!
数据挖掘——关联分析基础介绍(上)
The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!
The latest second edition of comic novels, listening to books, three in one, complete source code / integrated visa free interface / building tutorials / with acquisition interface
从2019 年开始,你一定停止使用了这个营销策略…
随机推荐
Getting started with caspin
MOS tube - rapid recovery application notes (II) [parameters and applications]
1. Mx6u driver development-2-led driver
Ssl== certificate related concepts
Why does the 20 bit address bus determine the storage space of 1MB
How to understand "page storage management scheme"
Let variable declaration feature of ES6 new feature and its case
关于双指针的思想总结
面试必备!TCP协议经典十五连问!
Deep understanding of browser caching mechanism (HTTP)
一文学透MySQL表的创建和约束
I.MX6U-驱动开发-2-LED驱动
Batch production and upload sales NFT opensea eth polygon
Solve the delay in opening the console of Google browser
"Strangers once met" Summer Street Shen Shuyan_ Xia Mo Shen Shuyan's latest chapter
In depth C language (4) -- definition and use of switch
Shopify卖家:EDM营销就要搭配SaleSmartly,轻松搞定转化率
[redis series] string data structure
With more than 5 years of work experience and a salary of 15K, would you accept it if you were me?
The function parameters of the new features of ES6 are assigned initial values and rest parameters