在JavaScript中复制所选文本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在JavaScript中复制所选文本相关的知识,希望对你有一定的参考价值。
Sometimes you have some information on your page and your visitors might want to copy it. The easiest way is to provide a mechanism that allows them to simply click a button to do so. You have to paste this code into the head of your web page:
function CopyText(el){ var selectedText = ""; if(window.getSelection){ selectedText = window.getSelection(); }else if (document.getSelection){ selectedText = document.getSelection(); }else if (document.selection){ selectedText = document.selection.createRange().text; } if(selectedText != ""){ selectedText = selectedText.toString(); el.focus(); el.value = selectedText; }else { alert("Select a text in the page and then press this button!"); } } </script> Select any part of this text to copy it... <form name="frmCopyText"> <textarea name="txtSelect" rows="4" cols="45"></textarea><br> <input onclick="CopyText(this.form.txtSelect)" type="button" value="Press to copy the highlighted text" name="btnCopy"> </form>
以上是关于在JavaScript中复制所选文本的主要内容,如果未能解决你的问题,请参考以下文章