少说废话,上代码:
// 复制文本内容到剪切板 function copyText(obj) { if (!obj) { return false; } var text; if (typeof(obj) == 'object') { if (obj.nodeType) { // DOM node obj = $(obj); // to jQuery object } try { text = obj.text(); if (!text) { // Maybe <textarea /> text = obj.val(); } } catch (err) { // as JSON text = JSON.stringify(obj); } } else { text = obj; } //var $temp = $('<input>'); // Line feed is not supported var $temp = $('<textarea>'); $('body').append($temp); $temp.val(text).select(); var res = document.execCommand('copy'); $temp.remove(); return res; }
调用示例:
<textarea id="taCode" rows="8" style="resize: none"></textarea> <button type="button" class="btn btn-primary" onclick="copyText($('#taCode'))">复制 方法一</button> <button id="btCopy" type="button" class="btn btn-primary">复制 方法二</button> <script type="text/javascript"> $("#btCopy").click(function() { //copyText($("#taCode").text()); copyText($("#taCode").val()); }); </script>
参考链接:https://github.com/by-syk/jquery-copy
未经允许不得转载:哈勃私语 » jQuery复制文本到剪切板
本文共859个字 创建时间:2019年6月6日16:56