实现的JS代码如下:
完整可运行代码如下:你也可以修改代码后再运行.
}
//对应于按键事件方法1
function keyPress1(event){
//当按下TAB键时,通过取消默认事件,并动态输入四个\t来实现TAB键缩进.
if(event.keyCode==9){
//取消默认事件
if(document.all){//IE
event.returnValue=false;
}else{//other(FF)
event.preventDefault();
}
var editor=document.getElementById("editor_position").contentWindow;
if(document.all){//IE
var rng=editor.document.selection.createRange();
rng.text="\t\t\t";
//rng.collapse(false);
//rng.select();
}else{//FF
var range=document.createRange();
range.text="ok";
var rng=editor.window.getSelection();
if(rng.rangeCount>0)rng.removeAllRanges();
rng.text="dovapour";
}
}
}
/*
IE中:keypress事件的keyCode能区分大小写,但不能识别功能键(Ctr,Shift,Alt等); keyup和keydown事件的keyCode不能区分大小写,能识别功能键.
FF中:keypress事件的keyCode=0; keyup和keydown事件的keyCode不能区分大小写,能识别功能键.
Chrome中:keypress事件的keyCode能区分大小写,但不能识别功能键(Ctr,Shift,Alt等); keyup和keydown事件的keyCode不能区分大小写,能识别功能键.
*/
//对应于按键事件方法2
function keyPress2(ev){
alert(ev.keyCode);
}
</script>
</head>
<body>
<table style="background-color:#ccc;" cellpadding=0 cellspacing=5 border=0 >
<tr>
<td>
<table id="td_btns">
<tr>
<td id="bold_td" title="加粗" onclick="editorFormat('bold');">
<img src="testImages/bold.gif" width="16" height="16" />
</td>
<td title="斜体" onclick="editorFormat('italic');">
<img src="testImages/italic.gif" width="16" height="16" />
</td>
<td title="下划线" onclick="editorFormat('underline');">
<img src="testImages/underline.gif" width="16" height="16" />
</td>
<td title="删除线" onclick="editorFormat('strikethrough');">
<img src="testImages/strikethrough.gif" width="16" height="16" />
</td>
<td title="取消格式" onclick="editorFormat('removeformat');">
<img src="testImages/removeformat.gif" width="16" height="16" />
</td>
<td title="左对齐" onclick="editorFormat('justifyleft');">
<img src="testImages/aleft.gif" width="16" height="16" />
</td>
<td title="中间对齐" onclick="editorFormat('justifycenter');">
<img src="testImages/acenter.gif" width="16" height="16" />
</td>
<td title="右对齐" onclick="editorFormat('justifyright');">
<img src="testImages/aright.gif" width="16" height="16" />
</td>
</tr>
</table>
</td>
</tr>
<tr height="4"><td></td></tr>
<tr height="400">
<td valign="top"><iframe ID="editor_position" marginheight="0" style="word-break:break-all; width:640px; overflow:hidden;" marginwidth="0" hspace="0" width="640" height="400"></iframe>
</td>
</tr>
</table>
</body>
</html>