Created
August 1, 2017 10:37
-
-
Save jiangzhuo/378bd2fe2da541d93b39c88281b9fa43 to your computer and use it in GitHub Desktop.
js操作剪切板
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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