Skip to main content

【h5】点击复制到粘贴板

  • 兼容性:安卓:ios
/**
* @desc 复制票号
* @param {}
* @return {}
*/
function copyTicketCodes() {
if (!selectTickets.length) {
Toast(t`请选择车票`);
return;
}
const text = selectTickets.join('、');
let inputDom = document.createElement('input');
inputDom.value = text;
inputDom.type = 'text';
document.body.appendChild(inputDom);
selectText(inputDom, 0, text.length);
if (document.execCommand('copy')) {
document.execCommand('copy')// 执行浏览器复制命令
Toast(t`已复制到粘贴板`);
} else {
Toast(t`不兼容`);
}
inputDom.className = 'oInput';
inputDom.style.display = 'none';
}

function selectText(textbox, startIndex, stopIndex) {
if (textbox.createTextRange) {//ie
var range = textbox.createTextRange();
range.collapse(true);
range.moveStart('character', startIndex);//起始光标
range.moveEnd('character', stopIndex - startIndex);//结束光标
range.select();//不兼容苹果
} else {//firefox/chrome
textbox.setSelectionRange(startIndex, stopIndex);
textbox.focus();
}
}