模仿开心网做的在线html编辑器

 转自:http://huqilong.blog.51cto.com/53638/115479   

     最近做社区,产品认准了开心网的编辑器,但是那个笔记其用到prototype框架,而我们使用的jquery-1.2.2.js,两者有冲突,于是一狠心,查了查资料,用了两天时间写了一个.
       刚开始查的资料,在线编辑器的原理大概如下:
       一、基于textarea  特点:简单,但是表情只能通过ubb进行控制,不能所见即所得
       二、基于div 特点:简单,所见即所得,比较复杂,但是兼容性不好,js不易控制.
       三、基于iframe,具有以上两者的优点,没有以上两者的缺点

那就用第三种了.

       附件中是实现的东西,本人js面向对象这块不是很熟悉,高人可以修改指点下,别忘了留言啊,可以的话给我也发一份:huqilong@126.com
       整个编辑器就一个js文件,大概不到200行js代码吧,如果需要 加粗、图片等功能,只要看下里面的另外一个文件即可。
       特点:实现了“开心网”编辑器所有功能
             代码量极少:101行js代码 js+css总共4k
             引用简单:两句代码搞定
             支持一个页面多个文本框应用
             面向对象的js封装
             简洁,无多余功能
             扩展简单:想要其他功能可以很easy地添加
             支持IE6、firefox、iE7

源码下载




使用方法如下:

     效果图:

    
   js代码

var  face_count = 50 ;
var  face_head = " images/ " ;
var  face_tail = " .gif " ;

function  checkBrowserType()
{
  
var  app = navigator.appName;
  
if  (app.indexOf( ' Microsoft ' !=   - 1 ) {
    
return   0 ;
  }
  
if  (app.indexOf( ' Netscape ' !=   - 1 ) {
    
return   1 ;
  }
}

function  EasyEditor(editorId){
    
this .editorId = editorId;
}
EasyEditor.prototype
= {    
    addFaceSelector:
function (){
    
var  face_selector_div = " <div><img src=\ "" +face_head+ " ec_1 " +face_tail+ " \ "  id=\ " faceSelector_ " +this.editorId+ " \ " /></div> " ;
    document.write(face_selector_div);
    
this .faceSelector = document.getElementById( " faceSelector_ " + this .editorId);
    },
    addFaceImageDiv:
function (){
      
var  facesDiv = " <div id=\ " facesDiv_ " +this.editorId+ " \ "  class=\ " faceDiv\ " > " ;      
    
for ( var  i = 1 ;i <= face_count;i ++ ){
        
var  _img = " <img src=\ "" +face_head+i+face_tail+ " \ "  id=\ " face_image_ " +this.editorId+ " _ " +i+ " \ "  class=\ " facesImg\ " /> " ;
        facesDiv
+= _img;        
    }
    facesDiv
+= " </div> " ;
    document.write(facesDiv);
    
this .facesDiv = document.getElementById( " facesDiv_ " + this .editorId);    
    },
    addFrame:
function (){
      
var  frameId = " frame_ " + this .editorId;
      
var  frameString = " <iframe frameborder=\ " 0 \ "  scrolling=\ " auto;\ "  marginwidth=\ " 0 \ "  id=\ "" +frameId+ " \ "  class=\ " editorFrame\ " ></iframe> " ;
      document.write(frameString);
      
this .frame = document.getElementById(frameId);
      
this .editorArea =   this .frame.contentWindow;
      
this .editorArea.document.designMode  =   " on " ;
      
this .editorArea.document.contentEditable  =   true
      
this .editorArea.document.open(); 
      
this .editorArea.document.writeln( ' <html><head> ' );
    
this .editorArea.document.writeln( " <style>body{cursor:text;background: #ffffff; margin:1px; padding:1px; font-size:14px; overflow:auto;word-wrap: break-word; font-family: Arial, \ " 宋体\ " ;} p {padding: 0px; margin: 0px;}</style> " );
    
this .editorArea.document.writeln( ' </head> ' );
    
this .editorArea.document.writeln( ' <body>        </body> ' );
    
this .editorArea.document.writeln( ' </html> ' );
      
this .editorArea.document.close();    
    },
    attachFaceAction:
function (){
        
for ( var  i = 1 ;i <= face_count;i ++ ){
            
var  imgObj = document.getElementById( " face_image_ " + this .editorId + " _ " + i);
            imgObj.insertFace
= this .insertFace;
            imgObj.insertHtml
= this .insertHtml;
            imgObj.editorArea
= this .editorArea;
        imgObj.setup
= this .setup;
        imgObj.facesDiv
= this .facesDiv;
            imgObj.onmouseover
= function (){ this .style.cursor = " pointer " ; this .style.border = " 1px solid #D01E3B " ;};
            imgObj.onmouseout
= function (){ this .style.border = " 1px solid #CCCCCC " ;};
        imgObj.onclick
= function (){ this .insertFace( this .src); this .facesDiv.style.display = " none " ;};
    }
    },
    setup:
function (command,content){
        
this .editorArea.focus();
        
this .editorArea.document.execCommand(command,  false , content);    
    },
    insertHtml:
function (content){
        
if (checkBrowserType() == 0 ){
          
this .editorArea.focus();
          
this .editorArea.document.selection.createRange().pasteHTML(content);
        }
else {            
          
this .setup( " insertHTML " ,content);
        }
    },
    getHtml:
function (){
        
return   this .editorArea.document.body.innerHTML;    
    },
    insertFace:
function (imgPath){
        
var  htmlContent = " <img src=\ "" +imgPath+ " \ " ></img> " ;
        
this .insertHtml(htmlContent);
    },    
 keyPress:
function (){
     alert(
" test " );
       
var  ev = this .editorArea.event;
    
if (ev.keyCode == 13 ){
        
this .insertHtml( " <br/> " );
        
return   false ;  
    }  
     
return   true ;  
  },
    init:
function (){
         
this .addFaceSelector();
         
this .addFaceImageDiv();
         
this .addFrame();
         
this .attachFaceAction();
          
this .faceSelector.facesDiv = this .facesDiv;
         
this .faceSelector.onmouseover = function (){ this .style.cursor = " pointer " ;};
         
this .faceSelector.onclick = function (){ var  divStyle = this .facesDiv.style;divStyle.display == " block " ? divStyle.display = " none " :divStyle.display = " block " ;};
    }
}


    html代码:

< html >
< head >
    
< link  href ="editor.css"  type ="text/css"  rel ="stylesheet" >
    
< script  language ="javascript"  src ="editor.js" ></ script >     
        
< script  language ='javascript' >
          
function  test()
          {
              alert( editor.getHtml());
          }
        
</ script >
</ head >
< body >
< script  language ="javascript" >
var  editor = new  EasyEditor( " editor1 " );
editor.init();
</ script >
< br />
< br />
< input  type ="button"  value ="测试"  id ="test"  onclick ='test();' />
</ body >
</ html >


=====================================================================

   改进篇

var  face_count = 50 ;
var  face_head = " images/ " ;
var  face_tail = " .gif " ;

function  checkBrowserType()
{
  
var  app = navigator.appName;
  
if  (app.indexOf( ' Microsoft ' !=   - 1 ) {
    
return   0 ;
  }
  
if  (app.indexOf( ' Netscape ' !=   - 1 ) {
    
return   1 ;
  }
}

function  EasyEditor(editorId){
    
this .editorId = editorId;
    
this .container = document.getElementById( this .editorId);
}
EasyEditor.prototype
= {    
    addFaceSelector:
function (){
        
var  selectorContainer = document.createElement( " div " );
        
var  selectorImg = document.createElement( " img " );
        selectorImg.src
= face_head + " ec_1 " + face_tail;
        selectorImg.id
= " faceSelector_ " + this .editorId;
        selectorContainer.appendChild(selectorImg);
        
this .container.appendChild(selectorContainer);
        
this .faceSelector = document.getElementById( " faceSelector_ " + this .editorId);
    },
    addFaceImageDiv:
function (){
      
var  facesDiv = document.createElement( " div " );      
      facesDiv.id
= " facesDiv_ " + this .editorId;
      facesDiv.className
= " faceDiv " ;
    
for ( var  i = 1 ;i <= face_count;i ++ ){
    
var  faceImg = document.createElement( " img " );    
    faceImg.src
= face_head + i + face_tail;
    faceImg.id
= " face_image_ " + this .editorId + " _ " + i;
    faceImg.className
= " facesImg " ;
    facesDiv.appendChild(faceImg);  
    
this .container.appendChild(facesDiv);     
    }
    
this .facesDiv = document.getElementById( " facesDiv_ " + this .editorId);    
    },
    addFrame:
function (){
        
var  frame = document.createElement( " iframe " );
        frame.id
= " frame_ " + this .editorId; 
        frame.frameborder
= " 0 " ;
        frame.scrolling
= " auto " ;
        frame.marginwidth
= " 0 " ;
        frame.id
= " frameId_ " + this .editorId;
        frame.className
= " editorFrame " ;
        
this .container.appendChild(frame);
        
this .frame = document.getElementById(frame.id);
      
this .editorArea =   this .frame.contentWindow;
      
this .editorArea.document.designMode  =   " on " ;
      
this .editorArea.document.contentEditable  =   true
      
this .editorArea.document.open(); 
      
this .editorArea.document.writeln( ' <html><head> ' );
    
this .editorArea.document.writeln( " <style>body{cursor:text;background: #ffffff;height:12px; margin:1px; padding:1px; font-size:14px; overflow:auto;word-wrap: break-word; font-family: Arial, \ " 宋体\ " ;} p {padding: 0px; margin: 0px;}</style> " );
    
this .editorArea.document.writeln( ' </head> ' );
    
this .editorArea.document.writeln( ' <body>        </body> ' );
    
this .editorArea.document.writeln( ' </html> ' );
      
this .editorArea.document.close();    
    },
    attachFaceAction:
function (){
        
for ( var  i = 1 ;i <= face_count;i ++ ){
            
var  imgObj = document.getElementById( " face_image_ " + this .editorId + " _ " + i);
            imgObj.insertFace
= this .insertFace;
            imgObj.insertHtml
= this .insertHtml;
            imgObj.editorArea
= this .editorArea;
        imgObj.setup
= this .setup;
        imgObj.facesDiv
= this .facesDiv;
            imgObj.onmouseover
= function (){ this .style.cursor = " pointer " ; this .style.border = " 1px solid #D01E3B " ;};
            imgObj.onmouseout
= function (){ this .style.border = " 1px solid #CCCCCC " ;};
        imgObj.onclick
= function (){ this .insertFace( this .src); this .facesDiv.style.display = " none " ;};
    }
    },
    setup:
function (command,content){
        
this .editorArea.focus();
        
this .editorArea.document.execCommand(command,  false , content);    
    },
    insertHtml:
function (content){
        
if (checkBrowserType() == 0 ){
          
this .editorArea.focus();
          
this .editorArea.document.selection.createRange().pasteHTML(content);
        }
else {            
          
this .setup( " insertHTML " ,content);
        }
    },
    getHtml:
function (){
        
return   this .editorArea.document.body.innerHTML;    
    },
    insertFace:
function (imgPath){
        
var  htmlContent = " <img src=\ "" +imgPath+ " \ " ></img> " ;
        
this .insertHtml(htmlContent);
    },    
 keyPress:
function (){
     alert(
" test " );
       
var  ev = this .editorArea.event;
    
if (ev.keyCode == 13 ){
        
this .insertHtml( " <br/> " );
        
return   false ;  
    }  
     
return   true ;  
  },
    init:
function (){
         
this .addFaceSelector();
         
this .addFaceImageDiv();
         
this .addFrame();
         
this .attachFaceAction();
          
this .faceSelector.facesDiv = this .facesDiv;
         
this .faceSelector.onmouseover = function (){ this .style.cursor = " pointer " ;};
         
this .faceSelector.onclick = function (){ var  divStyle = this .facesDiv.style;divStyle.display == " block " ? divStyle.display = " none " :divStyle.display = " block " ;};
    }
}

html代码: 

< html >
< head >
    
< link  href ="editor.css"  type ="text/css"  rel ="stylesheet" >
    
< script  language ="javascript"  src ="editor.js" ></ script >
</ head >
< body >
< br />
< div  id ="testDiv"  style ="background-color:red;" ></ div >
</ body >
</ html >

< script  language ="javascript" >
var  editor = new  EasyEditor( " testDiv " );
editor.init();
function  getHtml(){
 alert(editor.getHtml());    
}
</ script >

< href ="#"  onclick ="getHtml();" > 取值 </ a >

 

 

 

转载于:https://www.cnblogs.com/qiantuwuliang/archive/2009/06/26/1511432.html

因为许多原因,开心网址已经停止开发,这个程序从一开始只是作为学习asp的练习作, 只是一时兴趣,从没有想到会得到这么多朋友的支持,许多朋友现在还在使用着这个程序。从一开始的asp到后来生成html,中间有着许许多多的漏洞,给很多朋友带来了麻烦,希望朋友们能够原谅。 程序的代码一直都是杂乱无章,因为作者本身只是一个asp的外行,只是本着学习的兴趣和使用的方便,没有考虑到很多其他的问题。每次发布都是拿自己的网站代码匆匆发布,有着很多杂七杂八的广告,也给很多朋友带来了不便,对此再一次对大家道歉。 从一年前在自己的电脑上第一次装linux到现在,基本上没有再碰过asp的代码(php正在学习中)。很多命令已经陌生了,所以这次整理代码,可能会出现很多错误,希望大家能及时反应,以便及时更改,给这个程序画一个圆满的句号。 因为贴吧,网摘都是在以前网上流传的几个程序上进行的修改,原作者已经不详,很多语句都没有仔细的看,而且模板修改也比较麻烦,所以一直都没有随程序发布,很多朋友想要,这次一并送上,可能需要修改的地方很多,请大家仔细研究。这次最后发布的版本与作者自己的网站完全相同,可能有许多多余的东西,可以根据自己的需要进行删除。 Asp代码+Access数据库+fso组件,请在支持asp和fso组件的环境下运行 版权说明 开心网址FinalVersion版程序源码属于免费源码,所有人可以下载,修改,使用以及传播,源码中没有任何限制功能,但是出于对作者劳动的尊重,希望您能保留网页底部的版权信息。 开心网址程序目前没有收费想法,但所有把本程序,或修改自本程序的源码收费提供给其他网友的法,我们保留追究法律责任的权利。 开心网址FinalVersion版程序源码265风格的css文件和图片均来源于265网址导航(http://www.265.com)。 注意事项 后台管理登陆界面默认地址是:http://你的域名/admin 默认管理员帐号admin密码admin管理认证码happywz注意及时更改 为了网站安全,发布前一定要的事: 1.修改后台管理文件夹名(admin文件夹) 2.修改数据库地址(data文件夹名和#data.asa文件名) 3.将mdb.asp中的数据库地址改为修改后的数据库相对于根目录的地址,sessionvar改为任意字符(作为你网站的唯一标识符),修改管理认证码,如果你的网站fso组件名称与默认不同,则还要修改fso组件的名称。 4.理论上,网址分类是可以无限层,但是为了网页美观,左侧的分类列表现在只支持3级分类,所以不推荐建立3级以上的分类。 网址部分功能 1.可以设置网站的名称,地址,信箱,管理员,88*31LOGO图片,首页180*60LOGO图片,关键字,网站描述等基本信息。 2.广告信息管理功能,所有广告都可以通过开关设置显示或隐藏。广告类型支持图片,flash,js代码,以及自写代码。注意把广告Js调用代码放置在需要位置。 3.导航条可以从后台进行添加,删除或者修改的管理。 4.数据库管理功能,可以在线备份,恢复,压缩数据库,以及用Sql语句批量处理数据。 5.超级管理员功能,可以增加高级管理员和数据输入员,各有不同功能限制。 6.支持网址N级分类,管理员可以后台对类别,网址进行添加,删除,修改和审核管理。 7.可以设置登录模式为自助登录或邮箱登录. 8.生成html功能,审核新入网址后请更新网址列表,并按分类重新生成html网页。注意在添加修改网址及添加修改分类后重新生成一遍网页。 9.在线编辑网页功能,随心所欲的设计所有页面。 10.最新更新2万条网址数据 11.sql防注入功能,可以锁定ip。 12.某些网站为了安全,将FSO组件的名称进行更改以达到禁用FSO的目的。这样html生成功能将无法使用,如果你的网站属于这种情况,可以到mdb.asp中更改FSO组件名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值