日期:2014-05-16 浏览次数:20418 次
<html> <body> <div id="aa">好啦 写完这个就休息了 GOOD NIGHT!<divp> <script language="javaScript"> var div = document.getElementById("aa"); div.onmouseup = function() { var userSelection; if (window.getSelection) { // 现代浏览器 userSelection = window.getSelection(); } else if (document.selection) { // IE浏览器 userSelection = document.selection.createRange(); } userSelection.pasteHTML("<b>" + userSelection.text + "</b>"); }; </script> </body> </html>
------解决方案--------------------
<textarea style="width:200px;height:200px;border:1px solid #000" id="editor"> 321321321321312321 </textarea> <input type="button" onclick="func()" value="改变"/> <script type="text/javascript"> function func() { var e = document.getElementById("editor") if(document.selection){ var range = document.selection.createRange(); range.text = "["+range.text+"]"; } if(document.getSelection) { var start = e.selectionStart; var end = e.selectionEnd; e.value = e.value.substr(0, start) +"["+ e.value.substring(start,end)+"]" + e.value.substring(end, e.value.length); } } </script>