1. 程式人生 > >Html點選複製文字內容

Html點選複製文字內容

(function name(params) { 'use strict'; //新增點選事件 document.body.addEventListener('click', copy, true); //copy事件 function copy(e) { //找到要複製的區域 var t = e.target, c = t.dataset.copytarget, inp = (c ? document.querySelector(c) : null
); //要複製的區域是否可以選中 if (inp && inp.select) { //選中 inp.select(); try { //執行復制 document.execCommand('copy'); } catch (error) { alert('請使用Ctrl+c進行復制'); } } } })();