<a class="link" href="http://example.com" target="_blank">http://example.com</a> <input type="button" name="btnCopy" value="複製"> <script type="text/javascript"> $(function () { $("input[name=btnCopy]").click(function () { var node = document.querySelector('.link'); var range = document.createRange(); range.selectNode(node); window.getSelection().removeAllRanges(); window.getSelection().addRange(range); try { var successful = document.execCommand('copy'); if (successful) { alert("複製成功"); } else { alert("複製失敗"); } } catch (err) { alert("複製失敗!"); } window.getSelection().removeAllRanges(); }); }); </script>
參考:
沒有留言:
張貼留言