- <html>
- <button class="copy" type="submit">
- 复制链接
- </button>
- <script>
- function copyText(text) {
- var textarea = document.createElement("textarea");
- var currentFocus = document.activeElement;
- document.body.appendChild(textarea);
- textarea.value = text;
- textarea.focus();
- if (textarea.setSelectionRange)
- textarea.setSelectionRange(0, textarea.value.length);
- else
- textarea.select();
- try {
- var flag = document.execCommand("copy");
- } catch(eo){
- var flag = false;
- }
- document.body.removeChild(textarea);
- currentFocus.focus();
- return flag;
- }
- $(".copy").on('click',function(){
- var text='复制的文字';
- var flag = copyText(text);
- alert(flag ? "复制成功!" : "复制失败!");
- });
- </script>
- </html>