提示:您可以先修改部分代碼再運行
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.111cn.net/1999/xhtml" lang="zh-CN"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta http-equiv="Content-Language" content="zh-CN" /><title></title><script type="text/javascript">function $( id ) {var obj = null;obj = document.getElementById(id);return obj ? obj : null;}function show( id ) {$(id).style.display = "block";}function hide( id ){$(id).style.display = "none";}function MouseEvent(e) { this.e = e || window.event; this.x = e.pageX || e.clientX; this.y = e.pageY || e.clientY; this.target = e.target || e.srcElement;}function addListener(type, callback) { if (document.addEventListener) { document.addEventListener(type, callback, false); } else if (document.attachEvent) { document.attachEvent("on"+type, callback, false); }}function removeListener (type, callback) { if (document.removeEventListener) { document.removeEventListener(type, callback, false); } else if (document.detachEvent) { document.detachEvent("on" + type, callback, false); }}function handleDown() {if($('selected')) {$('selected').outerHTML = $('selected').innerHTML;hide('editbox');} addListener("mouseup", handleUp);}function handleUp(e) { var e = new MouseEvent(e);removeListener("mouseup", handleUp);if (window.getSelection) {alert('ݲִ֧IE鿴'); return false;//var str = window.getSelection().toString();} else if (document.getSelection) {alert('ݲִ֧IE鿴'); return false;//var str = document.getSelection().toString();} else if (document.selection) {//IEvar r = document.selection.createRange(); var str = r.text;} if(str != '' && e.target == $('edittext')) {r.execCommand('ForeColor', false, 'red');var regExp = /<font(.*)</font>/i;e.target.innerHTML = e.target.innerHTML.replace(regExp, '<span id="selected" >'+str+'</span>');show('editbox');$('editbox').style.left = e.x + "px";$('editbox').style.top = e.y + "px";$('textarea').value = str;}}function save() { var val = $('textarea').value; $('selected').outerHTML = val;hide('editbox');}</script></head><body><div id="edittext" onmousedown="handleDown();" >把選擇定內容增加到文字框</div><div id="editbox" style = "background: #ddd; position: absolute; border: 1px solid #aaa; width: 300px; height: 150px; padding: 5px; text-align: right; display: none;"><textarea id="textarea" style = "width:295px; height: 120px; border: 1px solid #aaa; margin-bottom: 3px;">
提示:您可以先修改部分代碼再運行