注意浏览器支持
.copy-to-clipboard { margin-bottom: torem(20px); .input { width: torem(260px); font-size: torem(12px); line-height: 1.5; border: 1px solid var(--grey); padding: torem(5px) torem(2px); } .btn { height: torem(30px); border: 1px solid var(--grey); padding: 0 torem(3px); margin-left: torem(10px); cursor: pointer; } }
/** * 点击事件处理函数 */ function handler(e) { const { copytarget } = e.target.dataset; const inp = copytarget ? document.querySelector(`#${copytarget}`) : null; if (inp && inp.select) { inp.select(); try { document.execCommand('copy'); inp.blur(); } catch (err) { console.log('please press Ctrl/Cmd+C to copy'); // eslint-disable-line no-console } } } document.addEventListener('DOMContentLoaded', () => { document.body.addEventListener('click', handler, false); });