使剪贴板复制粘贴在iphone设备上工作
Make clipboard copy-paste work on iphone devices
我有一个web应用程序,主要是为在移动设备上运行而设计的。我有一个按钮,这将复制到设备剪贴板传递的文本。我使用javascript。我的代码在所有移动设备上都运行良好,除了iphone和ipad。有人知道是什么问题吗?下面是我的代码:
CopyToClipboard = function(text, fallback){
var $t = $('<textarea />');
$t.val(text).appendTo('body');
$t.select();
document.execCommand('copy');
$t.remove();
return true;
};
我也试过这样做,但没有结果,仍然不能在iphone上工作
function detectIE() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
var edge = ua.indexOf('Edge/');
if (edge > 0) {
// IE 12 => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
}
// other browser
return false;
}
function copytext(text) {
if (detectIE()) {
window.clipboardData.setData('Text', text);
}
var textField = document.createElement('textarea');
textField.innerText = text;
document.body.appendChild(textField);
textField.select();
document.execCommand('copy');
window.clipboardData.setData('Text', copytext);
textField.remove();
}
function copytext(text) {
var textField = document.createElement('textarea');
textField.innerText = text;
document.body.appendChild(textField);
textField.select();
document.execCommand('copy');
$(textField).remove();
}
试试这个。
var copy = function(elementId) {
var input = document.getElementById(elementId);
var isiOSDevice = navigator.userAgent.match(/ipad|iphone/i);
if (isiOSDevice) {
var editable = input.contentEditable;
var readOnly = input.readOnly;
input.contentEditable = true;
input.readOnly = false;
var range = document.createRange();
range.selectNodeContents(input);
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
input.setSelectionRange(0, 999999);
input.contentEditable = editable;
input.readOnly = readOnly;
} else {
input.select();
}
document.execCommand('copy');
}
<input type="text" id="foo" value="text to copy" />
<button onclick="copy('foo')">Copy text</button>
根据CanIUse, Safari在iOS上不支持document.execCommand('copy')
,可能是出于安全原因。
相关文章:
- 数据rel=“;背面“;无法在iPhone上工作
- 显示:没有不工作的iPhone模拟器
- 在iPhone上,Vimeo Javascript API.play()函数没有'播放完视频后才能工作
- iCheck插件在ipad和iphone上无法正常工作
- jQuery AJAX刷新没有'不能在iPhone上工作
- 谷歌地图无法在iPhone / iPad工作灯6.2应用程序上运行
- 双击无法在iPhone safari浏览器中工作
- 流星:谷歌登录不'不能在iphone/ipad上工作
- 按钮在iPhone上无法正常工作(可能还有其他触摸屏)
- iPhone谷歌图像搜索触摸事件功能是如何工作的
- iPhone:span点击不'我工作不好
- 如何使相对位置像iPhone上的固定位置一样工作
- 网站响应能力-Iphone需要双击链接才能工作
- 响应式灯箱无法在iPhone中工作
- innerHTML不能在iPhone上工作
- mootools setProperty方法不能在iphone, ipad上工作
- 工作书签脚本的iphone/智能手机
- JavaScript子菜单在iPhone iOS 5更新后不再工作
- 使剪贴板复制粘贴在iphone设备上工作
- PHP多个cookie不能在iPad / iPhone浏览器上工作