中华网校

系列网站: 中华网校 | www.网校.com | 3D模型 | 中华网校教育

电脑网校 | 业界新闻 | 职业网校 | 网校宝典 | 软件下载 | 网校论坛 | 网校联盟

电脑入门 | 网页设计 | 网络编程 | 图形图象 | 三维空间 | 多媒体 | 程序语言 | 操作系统 | 系统专题 | 办公应用 | 软件宝典 | 硬件天下 | 

 

您的位置:首页 >> 网络编程 >> JSP >> 新闻正文

自己动手,结合javascript和dhtml做一个ubb编辑器(附例子代码)  

作者:_  时间:2004-7-10  来自:中华网校  责任编辑:  阅读次数:

看到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 buttonshadow";
thisTD.style .borderTop = "1px solid buttonhighlight";
thisTD.style .borderBottom = "1px solid buttonshadow";
}

function On_Mouseout(thisTD)
{
thisTD.style .borderLeft = "" ;
thisTD.style .borderRight = "";
thisTD.style .borderTop = "";
thisTD.style .borderBottom = "";
}

function DoDown(thisTD)
{
thisTD.style .borderLeft = "1px solid buttonshadow";
thisTD.style .borderRight = "1px solid buttonhighlight";
thisTD.style .borderTop = "1px solid buttonshadow";
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>

相关文章 最新文章 推荐文章
Sun Java Web Server 能让攻击者远程执行任意命令
Jboss下配置EJB
自己动手,结合javascript和dhtml做一个ubb编辑器(附例子代码)

  中华网校依法保护知识产权,如果我们的文章有涉及或侵犯您的有关权益,请即时与我们 联系, 注明网址及文章,我们会即时处理或删除,感谢您的合作!中华网校email
  中华网校由广州市中六电脑城智锐计算机专业培训学院及中华网校技术中心提供网络支持未经本站许可任何个人网站、书刊报社一律不得私自复制,转载本站内容!

关于中华网校 | 广告服务 | 版权声明 | 投稿指南 | 网站合作 | 友情链接 | 网站地图

 

版权所有 中华网校 & 智锐网校 1999-2004 COPYRIGHT (C) 1999-2004 www.ZhiRui.com ALL RIGHTS RESERVED

 
/**/