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

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
    看到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  οnclick=doaction("bold") οnmοusedοwn="dodown(tdbold );" onmouseover = "on_mouseover(tdbold) ;" οnmοuseοut="on_mouseout(tdbold);">
          <img src='bold.gif' width=16 height=16  >
       </td>
       <td id=tditalic οnclick=doaction("italic") οnmοusedοwn="dodown(tditalic);" onmouseover = "on_mouseover(tditalic) ;" οnmοuseοut="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>      
< <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值