结合javascript和dhtml做一个ubb编辑器
时间:2008-10-23
来源:互联网
看到chinaASP论坛的abc code editor了吗?是不是觉得很cool? 说真的,刚见到我还以为是用别的什么语言做的控件呢,后来才发现没有那么神秘的。前几天做一个商品bbs,客户要求支持ubb,同时也要做一个编辑器。现在我把做ubb的思路给大家讲一下。
首先遇到的是界面问题,实际上这个很好解决,只是利用td的onmouseover、onmouseout和onmousedown来实现,具体实现方法件下面的代码。
其次就是实现文本效果的问题,这个可以利用textRange的execCommand方法来实现。
下面我给出一个简单的例子,你可以把它存为一个html文件,直接可以运行,这个例子的功能很简单,就是把编辑框中选定的文字变为粗体或斜体。其他功能你可以参照这个例子自己加上。
file : ubb.html
<HTML>
<HEAD>
<TITLE>ubb演示</TITLE>
</HEAD>
<BODY>
<br><br>
<table width=300 cellspacing=2 cellpadding=2 border=0 bgcolor=lightgrey>
<tr>
<td id=tdBold onclick=doAction(\"Bold\") onmousedown=\"DoDown(tdBold );\" onmouseover = \"On_Mouseover(tdBold) ;\" onmouseout=\"On_Mouseout(tdBold);\">
<img src='bold.gif' width=16 height=16 >
</td>
<td id=tdItalic onclick=doAction(\"Italic\") onmousedown=\"DoDown(tdItalic);\" onmouseover = \"On_Mouseover(tdItalic) ;\" onmouseout=\"On_Mouseout(tdItalic);\">
<img src='italic.gif' width=16 height=16 >
</td>
<td width=268> </td>
</tr>
<tr>
<td colspan=3>
<iframe id=Editor name=Editor border=0 scroll=no width=300 height=200>
</iframe>
</td>
</tr>
</table>
</BODY>
</HTML>
<script language=javascript>
//initialize the iframe
Editor.document .designMode = \"On\" ;
Editor.document .open ;
Editor.document .write (\" \") ;
Editor.document .close ;
Editor.focus ();
function On_Mouseover(thisTD)
{
thisTD.style .borderLeft = \"1px solid buttonhighlight\" ;
thisTD.style .borderRight = \"1px solid butt****hadow\";
thisTD.style .borderTop = \"1px solid buttonhighlight\";
thisTD.style .borderBottom = \"1px solid butt****hadow\";
}
function On_Mouseout(thisTD)
{
thisTD.style .borderLeft = \"\" ;
thisTD.style .borderRight = \"\";
thisTD.style .borderTop = \"\";
thisTD.style .borderBottom = \"\";
}
function DoDown(thisTD)
{
thisTD.style .borderLeft = \"1px solid butt****hadow\";
thisTD.style .borderRight = \"1px solid buttonhighlight\";
thisTD.style .borderTop = \"1px solid butt****hadow\";
thisTD.style .borderBottom = \"1px solid buttonhighlight\";
thisTD.style .paddingTop = \"2px\";
thisTD.style .paddingLeft = \"2px\";
thisTD.style .paddingBottom = \"0px\";
thisTD.style .paddingRight = \"0px\";
}
function doAction(str)
{
var m_objTextRange = Editor.document .selection.createRange();
m_objTextRange.execCommand(str) ;
}
</script>
首先遇到的是界面问题,实际上这个很好解决,只是利用td的onmouseover、onmouseout和onmousedown来实现,具体实现方法件下面的代码。
其次就是实现文本效果的问题,这个可以利用textRange的execCommand方法来实现。
下面我给出一个简单的例子,你可以把它存为一个html文件,直接可以运行,这个例子的功能很简单,就是把编辑框中选定的文字变为粗体或斜体。其他功能你可以参照这个例子自己加上。
file : ubb.html
<HTML>
<HEAD>
<TITLE>ubb演示</TITLE>
</HEAD>
<BODY>
<br><br>
<table width=300 cellspacing=2 cellpadding=2 border=0 bgcolor=lightgrey>
<tr>
<td id=tdBold onclick=doAction(\"Bold\") onmousedown=\"DoDown(tdBold );\" onmouseover = \"On_Mouseover(tdBold) ;\" onmouseout=\"On_Mouseout(tdBold);\">
<img src='bold.gif' width=16 height=16 >
</td>
<td id=tdItalic onclick=doAction(\"Italic\") onmousedown=\"DoDown(tdItalic);\" onmouseover = \"On_Mouseover(tdItalic) ;\" onmouseout=\"On_Mouseout(tdItalic);\">
<img src='italic.gif' width=16 height=16 >
</td>
<td width=268> </td>
</tr>
<tr>
<td colspan=3>
<iframe id=Editor name=Editor border=0 scroll=no width=300 height=200>
</iframe>
</td>
</tr>
</table>
</BODY>
</HTML>
<script language=javascript>
//initialize the iframe
Editor.document .designMode = \"On\" ;
Editor.document .open ;
Editor.document .write (\" \") ;
Editor.document .close ;
Editor.focus ();
function On_Mouseover(thisTD)
{
thisTD.style .borderLeft = \"1px solid buttonhighlight\" ;
thisTD.style .borderRight = \"1px solid butt****hadow\";
thisTD.style .borderTop = \"1px solid buttonhighlight\";
thisTD.style .borderBottom = \"1px solid butt****hadow\";
}
function On_Mouseout(thisTD)
{
thisTD.style .borderLeft = \"\" ;
thisTD.style .borderRight = \"\";
thisTD.style .borderTop = \"\";
thisTD.style .borderBottom = \"\";
}
function DoDown(thisTD)
{
thisTD.style .borderLeft = \"1px solid butt****hadow\";
thisTD.style .borderRight = \"1px solid buttonhighlight\";
thisTD.style .borderTop = \"1px solid butt****hadow\";
thisTD.style .borderBottom = \"1px solid buttonhighlight\";
thisTD.style .paddingTop = \"2px\";
thisTD.style .paddingLeft = \"2px\";
thisTD.style .paddingBottom = \"0px\";
thisTD.style .paddingRight = \"0px\";
}
function doAction(str)
{
var m_objTextRange = Editor.document .selection.createRange();
m_objTextRange.execCommand(str) ;
}
</script>
作者: 游人 发布时间: 2008-10-23

作者: ahsxw255 发布时间: 2008-10-23
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28