textarea
,把需要的文本放进 textarea
中textarea
元素插入 body
中。HTMLInputElement.select()
方法选择 textarea
中的文本内容document.execCommand('copy')
复制 textarea
中的文本内容到剪贴板body
删除 textarea
元素const copyToClipboard = str => {
const el = document.createElement('textarea');
el.value = str;
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
};
const copyToClipboard = str => {
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
};
DocumentOrShadowRoot.getSelection(), Selection.rangeCount, Selection.getRangeAt(), Selection.removeAllRanges() and Selection.addRange()
这些方法存储用户选择的文本内容和解决范围选择的问题const copyToClipboard = 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
}
};
function createNode(text) {
const node = document.createElement('div');
node.innerText = text;
node.style.cssText = 'position:absolute; top: 0; left: 0; height:0; width:0; pointer-events: none;';
document.body.appendChild(node);
return node;
}
export default function copyMe(text) {
const targetNode = createNode(text);
const range = document.createRange();
const selection = window.getSelection()!;
const selected = selection.rangeCount > 0
? selection.getRangeAt(0)
: false;
targetNode.focus(); // focus 我们需要的文本
range.selectNodeContents(targetNode);
if(selected){
selection.removeAllRanges();
selection.addRange(range);
}
let result;
try {
result = document.execCommand('copy');
} catch (e) {
result = false;
}
document.body.removeChild(targetNode);
return result;
}
import React, { Fragment } from 'react';
import copyMe from 'utils/copyMe';
interface ItemProps {
value?: string | number;
}
const Item: React.FC<ItemProps> = props => {
const { value } = props;
const copyme = () => {
alert(copyMe(value) ? 'Copied!' : 'Failed!');
};
return (
<Fragment>
{value && (
<div>
{value}
<textarea value={value} readOnly></textarea>
<span onClick={copyme}></span>
</div>
)}
</Fragment>
);
};
export default Item;