相容Android 和 ios JavaScript copy paste

來源:互聯網
上載者:User

標籤:out   ado   max   attr   erro   read   sel   for   mes   

<!DOCTYPE html><html><head><title>關於我們Frame</title><meta charset="utf-8"><meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/><meta name="format-detection" content="telephone=no,email=no,date=no,address=no"><meta name="copyright" content="www.acc5.com" /><style></style><script src="http://www.acc5.com/xueli/js/jquery-1.11.3.js"></script><script src="http://app-static.acc5.com/app/copy2clipboard.js"></script></head><body>hello ,welcome!!!<input id="test" type=‘text‘ value=‘test text144‘ /><br><button onclick="select_all_and_copy(document.getElementById(‘test‘))">Copy to Clipboard</button></body><script>$().ready(function(){$(‘#test‘).val(‘test‘+Math.random());try{  document.execCommand(‘copy‘);}catch(e){}})/*function iosCopyToClipboard(el) {var oldContentEditable = el.contentEditable,oldReadOnly = el.readOnly,range = document.createRange();el.contenteditable = true;el.readonly = false;range.selectNodeContents(el);var s = window.getSelection();s.removeAllRanges();s.addRange(range);el.setSelectionRange(0, 999999); // A big number, to cover anything that could be inside the element.el.contentEditable = oldContentEditable;el.readOnly = oldReadOnly;document.execCommand(‘copy‘);}iosCopyToClipboard(document.getElementById(‘test‘))*/</script></html>

 

function tooltip(el, message){var scrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;var x = parseInt(el.getBoundingClientRect().left) + scrollLeft + 10;var y = parseInt(el.getBoundingClientRect().top) + scrollTop + 10;if (!document.getElementById("copy_tooltip")){var tooltip = document.createElement(‘div‘);tooltip.id = "copy_tooltip";tooltip.style.position = "absolute";tooltip.style.border = "1px solid black";tooltip.style.background = "#dbdb00";tooltip.style.opacity = 1;tooltip.style.transition = "opacity 0.3s";tooltip.style.zIndex = "1999999999"; // Version 1.2bdocument.body.appendChild(tooltip);}else{var tooltip = document.getElementById("copy_tooltip")}tooltip.style.opacity = 1;tooltip.style.display = "block"; // Version 1.2btooltip.style.left = x + "px";tooltip.style.top = y + "px";tooltip.innerHTML = message;setTimeout(function() { tooltip.style.display = "none"; tooltip.style.opacity = 0; }, 2000);}function paste(el) {   if (window.clipboardData) {    // IE    el.value = window.clipboardData.getData(‘Text‘);    el.innerHTML = window.clipboardData.getData(‘Text‘);    }    else if (window.getSelection && document.createRange) {        // non-IE        if (el.tagName.match(/textarea|input/i) && el.value.length < 1)        el.value = " "; // iOS needs element not to be empty to select it and pop up ‘paste‘ button        else if (el.innerHTML.length < 1)        el.innerHTML = " "; // iOS needs element not to be empty to select it and pop up ‘paste‘ button        var editable = el.contentEditable; // Record contentEditable status of element        var readOnly = el.readOnly; // Record readOnly status of element       el.contentEditable = true; // iOS will only select text on non-form elements if contentEditable = true;       el.readOnly = false; // iOS will not select in a read only form element        var range = document.createRange();        range.selectNodeContents(el);        var sel = window.getSelection();        sel.removeAllRanges();        sel.addRange(range);         if (el.nodeName == "TEXTAREA" || el.nodeName == "INPUT")         el.select(); // Firefox will only select a form element with select()        if (el.setSelectionRange && navigator.userAgent.match(/ipad|ipod|iphone/i))        el.setSelectionRange(0, 999999); // iOS only selects "form" elements with SelectionRange        if (document.queryCommandSupported("paste"))        {  var successful = document.execCommand(‘Paste‘);      if (successful) tooltip(el, "Pasted.");    else {if (navigator.userAgent.match(/android/i) && navigator.userAgent.match(/chrome/i)){tooltip(el, "Click blue tab then click Paste");if (el.tagName.match(/textarea|input/i)){        el.value = " "; el.focus();        el.setSelectionRange(0, 0);         }        else         el.innerHTML = "";}elsetooltip(el, "Press CTRL-V to paste");}   } else {      if (!navigator.userAgent.match(/ipad|ipod|iphone|android|silk/i))tooltip(el, "Press CTRL-V to paste"); } el.contentEditable = editable; // Restore previous contentEditable status        el.readOnly = readOnly; // Restore previous readOnly status    }}function select_all_and_copy(el) {    // Copy textarea, pre, div, etc.if (document.body.createTextRange) {        // IE         var textRange = document.body.createTextRange();        textRange.moveToElementText(el);        textRange.select();        textRange.execCommand("Copy");   tooltip(el, "Copied!");      }else if (window.getSelection && document.createRange) {        // non-IE        var editable = el.contentEditable; // Record contentEditable status of element        var readOnly = el.readOnly; // Record readOnly status of element       el.contentEditable = true; // iOS will only select text on non-form elements if contentEditable = true;       el.readOnly = false; // iOS will not select in a read only form element        var range = document.createRange();        range.selectNodeContents(el);        var sel = window.getSelection();        sel.removeAllRanges();        sel.addRange(range); // Does not work for Firefox if a textarea or input        if (el.nodeName == "TEXTAREA" || el.nodeName == "INPUT")         el.select(); // Firefox will only select a form element with select()        if (el.setSelectionRange && navigator.userAgent.match(/ipad|ipod|iphone/i))        el.setSelectionRange(0, 999999); // iOS only selects "form" elements with SelectionRange        el.contentEditable = editable; // Restore previous contentEditable status        el.readOnly = readOnly; // Restore previous readOnly status     if (document.queryCommandSupported("copy"))    {var successful = document.execCommand(‘copy‘);      if (successful) tooltip(el, "Copied to clipboard.");    else tooltip(el, "Press CTRL+C to copy");}else{if (!navigator.userAgent.match(/ipad|ipod|iphone|android|silk/i))tooltip(el, "Press CTRL+C to copy");}    }} // end function select_all_and_copy(el) function make_copy_button(el){//var copy_btn = document.createElement(‘button‘);//copy_btn.type = "button";var copy_btn = document.createElement(‘span‘);copy_btn.style.border = "1px solid black";copy_btn.style.padding = "5px";copy_btn.style.cursor = "pointer";copy_btn.style.display = "inline-block";copy_btn.style.background = "lightgrey";copy_btn.setAttribute("role", "button");copy_btn.setAttribute("tabindex", 0);el.parentNode.insertBefore(copy_btn, el.nextSibling);copy_btn.onclick = function() { select_all_and_copy(el); };//if (document.queryCommandSupported("copy") || parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2]) >= 42)// Above caused: TypeError: ‘null‘ is not an object (evaluating ‘navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2]‘) in Safariif (document.queryCommandSupported("copy")){// Desktop: Copy works with IE 4+, Chrome 42+, Firefox 41+, Opera 29+// Mobile: Copy works with Chrome for Android 42+, Firefox Mobile 41+//copy_btn.value = "Copy to Clipboard";copy_btn.innerHTML = "Copy to Clipboard";}else{// Select only for Safari and older Chrome, Firefox and Opera/* Mobile:Android Browser: Selects all and pops up "Copy" buttoniOS Safari: Selects all and pops up "Copy" buttoniOS Chrome: Form elements: Selects all and pops up "Copy" button *///copy_btn.value = "Select All";copy_btn.innerHTML = "Select All";}}/* Note: document.queryCommandSupported("copy") should return "true" on browsers that support copybut there was a bug in Chrome versions 42 to 47 that makes it return "false".  So in thoseversions of Chrome feature detection does not work!See https://code.google.com/p/chromium/issues/detail?id=476508*/

  

from:http://www.seabreezecomputers.com/tips/copy2clipboard.htm

from:https://stackoverflow.com/questions/34045777/copy-to-clipboard-using-javascript-in-ios

相容Android 和 ios JavaScript copy paste

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.