Skip to content

Instantly share code, notes, and snippets.

@jiangzhuo
Created August 1, 2017 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiangzhuo/378bd2fe2da541d93b39c88281b9fa43 to your computer and use it in GitHub Desktop.
Save jiangzhuo/378bd2fe2da541d93b39c88281b9fa43 to your computer and use it in GitHub Desktop.
js操作剪切板
<!DOCTYPE html>
<html lang="en">
<head>
<script
src="http://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<meta charset="UTF-8">
<title>test iOS</title>
</head>
<body>
<!--<textarea id="textarea1"></textarea>-->
<input type="text" id="textarea1">
<script type="text/javascript">
document.getElementById('textarea1').addEventListener('click', function(){
var $input = $('#textarea1');
$input.val('kizuna.ai是人工智障!');
if (navigator.userAgent.match(/ipad|ipod|iphone/i)) {
var el = $input.get(0);
var editable = el.contentEditable;
var readOnly = el.readOnly;
el.contentEditable = true;
el.readOnly = false;
var range = document.createRange();
range.selectNodeContents(el);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
el.setSelectionRange(0, 999999);
el.contentEditable = editable;
el.readOnly = readOnly;
} else {
$input.select();
}
var res = document.execCommand('copy');
console.log(res)
$input.val('');
$input.blur();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment