1. 程式人生 > >手機實現點選複製的坑

手機實現點選複製的坑

點選複製有三種方式兩種外掛另一種是原生的寫(我比較建議用原生寫)

目前關於點選複製的外掛我知道的有ZeroClipboard.Core.jsclipboard.min.js

ZeroClipboard.Core.js

看了好多這個依附於flash

clipboard.min.js

安卓手機沒問題,測試蘋果六不相容點選沒反應

上程式碼

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="text" id="sourceText" style="position: fixed; top:-200px;" >
<div onclick="copywx()">點選複製</div>
<script>
var sourceText = document.getElementById('sourceText');
sourceText.preventDefault=false;
document.activeElement.blur();
sourceText.value=stxlwx;

function copywx(){
var currentFocus = document.activeElement;
sourceText.focus();
sourceText.setSelectionRange(0, sourceText.value.length);
document.execCommand('copy', true);
sourceText.blur();
confirm('微訊號'+stxlwx+'複製成功');
}
</script>
</body>
</html>