点击按钮实现复制指定文本
特别是做资源站,有提取码之类的字段 还是挺管用。分享给大家。
复制文本框中的文字
HTML代码部分
1 2 3 4 | <div> <input type="text" value="The Text to Copy" id="copyMe"> <button onclick="copyMyText()">复制到剪贴板</button> </div> |
JavaScript 部分
1 2 3 4 5 6 7 8 9 10 | <script> function copyMyText() { //select the element with the id "copyMe", must be a text box var textToCopy = document.getElementById("copyMe"); //select the text in the text box textToCopy.select(); //copy the text to the clipboard document.execCommand("copy"); } </script> |
由于上面使用select()选择文本框中的所有文本只适用于文本框,因此如果要将所有文本复制到特定ID或类名的剪贴板,则必须创建文本选择范围。
这有点灵活,因为可以不用文本框。
复制一段文字
HTML 代码部分
1 2 | div class="CopyMeClass" id="CopyMeID">这段文字会复制到剪贴板.</div> <div><button onclick="CopyClassText()">复制这段文字</button></div> |
Javascript 部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <script> function CopyClassText(){ //select the element with ID = "CopyMeID", can be a div, p, or a span, possibly others var textToCopy = document.getElementById("CopyMeID"); //you can target the same element using querySelector() as well //example below: //var textToCopy = document.querySelector('.CopyMeClass'); //check and see if the user had a text selection range var currentRange; if(document.getSelection().rangeCount > 0) ;{ //the user has a text selection range, store it currentRange = document.getSelection().getRangeAt(0); //remove the current selection window.getSelection().removeRange(currentRange); } else { //they didn't have anything selected currentRange = false; } //create a selection range var CopyRange = document.createRange(); //choose the element we want to select the text of CopyRange.selectNode(textToCopy); //select the text inside the range window.getSelection().addRange(CopyRange); //copy the text to the clipboard document.execCommand("copy"); //remove our selection range window.getSelection().removeRange(CopyRange); //return the old selection range if(currentRange) { window.getSelection().addRange(currentRange); } } </script> |
上面两种都是可以看到的文字,那么如何复制一段页面上不显示的文字呢。
复制一段页面上不显示的文字
HTML部分
1 | <p><button onclick="AdvancedCopy()">复制一段看不见的文字!</button></p> |
JavaScript 部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <script> function AdvancedCopy(){ //the text that is to be copied to the clipboard var theText = '这段文字会被复制到剪贴板'; //create our hidden div element var hiddenCopy = document.createElement('div'); //set the innerHTML of the div hiddenCopy.innerHTML = theText; //set the position to be absolute and off the screen hiddenCopy.style.position = 'absolute'; hiddenCopy.style.left = '-9999px'; //check and see if the user had a text selection range var currentRange; if(document.getSelection().rangeCount > 0) { //the user has a text selection range, store it currentRange = document.getSelection().getRangeAt(0); //remove the current selection window.getSelection().removeRange(currentRange); } else { //they didn't have anything selected currentRange = false; } //append the div to the body document.body.appendChild(hiddenCopy); //create a selection range var CopyRange = document.createRange(); //set the copy range to be the hidden div CopyRange.selectNode(hiddenCopy); //add the copy range window.getSelection().addRange(CopyRange); //since not all browsers support this, use a try block try { //copy the text document.execCommand('copy'); } catch(err) { window.alert("Your Browser Doesn't support this! Error : " + err); } //remove the selection range (Chrome throws a warning if we don't.) window.getSelection().removeRange(CopyRange); //remove the hidden div document.body.removeChild(hiddenCopy); //return the old selection range if(currentRange) { window.getSelection().addRange(currentRange); } } </script> |
好了,结束!