当前位置:网站首页>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()
}
}
边栏推荐
- Raft protocol - process demonstration
- (2022 Hangdian multi school III) 1011 link is as bear (thinking + linear basis)
- Data too long for column 'xxx' at row 1 solution
- Cannot paste multiple pictures at once
- [introduction to C language] zzulioj 1031-1035
- 代码 ~ 隐藏或禁用状态栏和虚拟按键
- HCIP BGP
- About the writing of ALV format control part
- 向日葵资深产品总监技术分享:“国民远控”如何在AD域环境下应用
- How fast does it take to implement a super simple language
猜你喜欢

UCOS task switching process

Typescript from getting started to mastering (XVI) configuration file - first knowledge of compileroptions configuration item

CUB_200鸟类数据集关键点可视化

UCOS任务切换过程

Beijing post network research 2015 problem2

SQL窗口函数

Microcomputer principle and interface technology

Since 2019, you must have stopped using this marketing strategy

Connect with third-party QQ login

Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators
随机推荐
Analysis of new retail o2o e-commerce model
About the writing of ALV format control part
Shopify卖家:EDM营销就要搭配SaleSmartly,轻松搞定转化率
HCIP BGP
力扣面试题17.04 消失的数字||260.只出现一次的数字(内含位运算知识点)
What have I learned from 200 machine learning tools?
CUB_ Visualization of key points in 200 bird dataset
【redis系列】字符串数据结构
UCOS任务切换过程
Remote desktop connection error
当我从数据库获取到了winfrom特定的控件ID之后我需要通过这个ID找到对应的控件,并对控件的TEXT文本进行赋值这该怎么做
nacos注册中心
1. 头文件-注释-命名空间-标准输入输出流
Process tracking of ribbon principle
Data too long for column 'xxx' at row 1 solution
向日葵远程控制为何采用BGP服务器?自动最优路线、跨运营商高速传输
Uni app internationalization
LDP --- 标签分发协议
LDP -- label distribution protocol
"Strangers once met" Summer Street Shen Shuyan_ Xia Mo Shen Shuyan's latest chapter