1. 程式人生 > >js複製文案-移動端可用

js複製文案-移動端可用

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
<div>
<span id="copyMy"> 複製我試試</span>
<button onClick="copyFn()">點選複製</button>
</div>

<script>
function copyFn(){
var val = document.getElementById('copyMy');
window.getSelection().selectAllChildren(val);
document.execCommand ("Copy");

}
</script>
</body>
</html>