兼容Android 和 ios JavaScript copy paste
<!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.2b
document.body.appendChild(tooltip);
}
else
{
var tooltip = document.getElementById("copy_tooltip")
}
tooltip.style.opacity = 1;
tooltip.style.display = "block"; // Version 1.2b
tooltip.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 = ""; }
else
tooltip(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 Safari
if (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" button
iOS Safari: Selects all and pops up "Copy" button
iOS 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 copy
but there was a bug in Chrome versions 42 to 47 that makes it return "false". So in those
versions 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的更多相关文章
- LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android
LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android jincon 发表于 2015-02-26 18:31:01 发表在: php开发 localresiz ...
- 【移动端兼容问题研究】javascript事件机制详解(涉及移动兼容)
前言 这篇博客有点长,如果你是高手请您读一读,能对其中的一些误点提出来,以免我误人子弟,并且帮助我提高 如果你是javascript菜鸟,建议您好好读一读,真的理解下来会有不一样的收获 在下才疏学浅, ...
- GitHub Android 最火开源项目Top20 GitHub 上的开源项目不胜枚举,越来越多的开源项目正在迁移到GitHub平台上。基于不要重复造轮子的原则,了解当下比较流行的Android与iOS开源项目很是必要。利用这些项目,有时能够让你达到事半功倍的效果。
1. ActionBarSherlock(推荐) ActionBarSherlock应该算得上是GitHub上最火的Android开源项目了,它是一个独立的库,通过一个API和主题,开发者就可以很方便 ...
- 兼容安卓和ios实现一键复制内容到剪切板
实现代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <m ...
- HTML5能取代Android和iOS应用程序吗?
大量新生移动设备的兴起,改变了互联网的未来.在技术的发展上,HTML5会取代App应用吗?或者说能够在多大程度上取代呢?在HTML5规范中,已经加入了相机.磁力罗盘.GPS信息的支持.很多新兴浏览器也 ...
- H5嵌入原生开发小结----兼容安卓与ios的填坑之路
一开始听说开发H5,以为就是做适配现代浏览器的移动网页,心想不用管IE了,欧也.到今天,发现当初too young too simple,兼容IE和兼容安卓与IOS,后者让你更抓狂.接下来数一下踩过的 ...
- 现在创业做App,先做 Android 还是 iOS?
随着互联网+的高速发展,现在创业大部分都是在布局移动端,初期往往摆在面前最大的难题是,如何分配有限的成本,在最快的速度内占领市场?这个大难题会影响创始人在团队和产品建设方方面面的决定.缩小至移动App ...
- 判断手机端用户打开页面时是android还是ios,并将判断结果通过ajax返回给url接口,传递回去
首先判断页面是android还是ios,然后利用ajax将结果通过接口url返回回去,记录到log日志中,以统计android和ios用户访问该页面的数量(数据统计) <script type= ...
- ArcGIS Runtime SDKs v10.2.4最新(Android、iOS、OSX和.NET)
ArcGIS Runtime SDKs v10.2.4最新,它包含:Android.iOS.OS X和.NET四大平台,用户和开发人员可以登录Esri最新的SDK安装包.或者通过云盘下载(http:/ ...
随机推荐
- C#设计模式之十二代理模式(Proxy Pattern)【结构型】
一.引言 今天我们要讲[结构型]设计模式的第七个模式,也是“结构型”设计模式中的最后一个模式,该模式是[代理模式],英文名称是:Proxy Pattern.还是老套路,先从名字上来看看.“代理”可以理 ...
- JVM虚拟机深入理解+GC回收+类加载
旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/xuri24/article/details/81455449 一,前言 本文章是读了“深入理 ...
- 字符串拼接引发的BUG
译者按: bug虽小,却是个磨人的小妖精! 原文: Fixing a bug: when concatenated strings turn into numbers in JavaScript 译者 ...
- JavaScript中的let和const
在ES6之前,JavaScript中只有两种作用域:全局作用域和函数内部的局部作用域.ES6中新增了两个重要的关键字,let和const,从而引入了块级作用域. 关键字var 使用var关键字声明的变 ...
- IE浏览器的ActiveXObject对象以及FileSystemobject的应用扩展(完成)
ActiveXObject 对象 启用和返回对自动化对象的引用.此对象仅用于实例化自动化对象,且此对象没有成员. 警告:此对象为 Microsoft 扩展,仅在 Internet Explorer 中 ...
- JavaScript之Object对象常用属性与方法手册
MDN Object参考地址:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Obje ...
- 洛谷P3245 [HNOI2016]大数(莫队)
题意 题目链接 Sol 莫队板子题.. 维护出每个位置开始的字符串\(mod P\)的结果,记为\(S_i\) 两个位置\(l, r\)满足条件当且仅当\(S_l - S_r = 0\),也就是\(S ...
- mybatis 通过实体类进行查询
如果使用实体类进行查询, 不管会不会使用到 主键, 都必须设置主键, 才能查询 <select id="selectByAppidAndServerCode" resultM ...
- 六. Redis发布订阅机制
发布订阅(pub/sub)是一种消息通信模式,主要是解除消息发布者和消息订阅者之间通信的耦合. Redis作为一个pub/sub的服务器,在订阅者和发布者之间起到了一个消息路由的功能.订阅者可以通过s ...
- java--反射机制总结
一.什么是Java反射机制? JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的以及动态调用对象的方法的功能 ...