Copy code The code is as follows: new document <br>body { font-size:12px; }<br>#show { background-color:#CCFF99; }<br></ style><br></head><br><body> <br><textarea id="content" cols="30" rows="10"><br>Fish died mysteriously in the river, downstream Residents are frequently suffering from strange diseases, and coastal plants are constantly mutating. Is it due to residual pesticides? Or a biochemical attack? Please pay attention to tonight's CCTV-10 "Scientific Exploration", the upcoming special program: "The Mysterious Foot Washer by the River-Chinese Men's Football Team" <br></textarea> <br> <button id="btn">Get selected value</button><p><div id="show"></div><br><script> <br> String .prototype.trim = function() { <br> return this.replace(/^s |s $/g, ""); <br> } <br> /* There are some problems with method 1 FF*/<br> function getSelectText() { <br> try{<br> // IE: document.selection.createRange() W3C:window.getSelection()<br> var selectText = (document.selection && document.selection.createRange)? document .selection.createRange().text : window.getSelection().toString();<br> if(selectText != null && selectText.trim() != ""){<br> <br> }catch(err){}<br> } } <br> /* Method 2*/<br> function getSelectText2(id) {<br> var t = document.getElementById(id);<br> if( window.getSelection) {<br> if(t.selectionStart != undefined && t.selectionEnd != undefined) {<br> return t.value.substring(t.selectionStart, t.selectionEnd);<br> } else { <br> return "";<br> }<br> } else {<br> return document.selection.createRange().text;<br> }<br> }<br> document.getElementById('btn') .onclick = function() {<br> document.getElementById('show').innerHTML = getSelectText2('content');<br> }<br></script><br></body> <br></html> <br><br><br></p> </div>