<script type="text/javascript"> var xmlhttp; var doneStr = loadXMLDoc('https://www.cnblogs.com/liudongpei/p/6021170.html'); function loadXMLDoc(url) { var htmldata; xmlhttp = null; if (window.XMLHttpRequest) { // code for IE7, Firefox, Opera, etc. xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } if (xmlhttp != null) { xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4) { // 4 = "loaded" if (xmlhttp.status == 200) { // 200 = "OK" htmldata = get_cnblogs_post_body(xmlhttp.responseText); } else { alert("Problem retrieving XML data:" + xmlhttp.statusText); } } } xmlhttp.open("GET", url, false); xmlhttp.send(null); } else { alert("Your browser does not support XMLHTTP."); } return htmldata; } function state_Change() { if (xmlhttp.readyState == 4) { // 4 = "loaded" if (xmlhttp.status == 200) { // 200 = "OK" //document.getElementById('A1').innerHTML=xmlhttp.status; //document.getElementById('A2').innerHTML=xmlhttp.statusText; //document.getElementById('A3').innerHTML=xmlhttp.responseText; console.log(typeof xmlhttp.responseText); htmldata = get_cnblogs_post_body(xmlhttp.responseText); } else { alert("Problem retrieving XML data:" + xmlhttp.statusText); } } } function get_cnblogs_post_body(str) { var start = str.indexOf("cnblogs_post_body") + 41; var stop = str.indexOf("MySignature") - 15; var getstr = str.substring(start, stop); // 用於提取字符串中介於兩個指定下標之間的字符。 //console.log(getstr); return getstr; } window.onload = function() { var newTextarea = document.createElement("textarea"); newTextarea.id = "theCode"; newTextarea.style = "width: 70%; height: 500px;"; newTextarea.value = doneStr; //document.body.appendChild(newTextarea); document.body.querySelector("div").appendChild(newTextarea); //*js複製 document.getElementById('doCopy').addEventListener('click', function() { var cText = document.getElementById('theCode'); var currentFocus = document.activeElement; cText.setSelectionRange(0, cText.value.length); document.execCommand('copy', true); //currentFocus.focus(); }); }; </script> <div> </div> <p> <input id="doCopy" type="button" value="複製代碼"> <input onclick="console.log('theCode')" type="button" value="複製代碼"> </p> javascript