站长中国
设为首页 | 站长论坛

站长论坛 站长下载
您所在的位置: 站长中国 > 站长学院 > 网页设计 > 网页特效 > 正文

教学演示-UBB,剪贴板,textRange及其他
  2007年11月17日04:02:01  评论(0条) 字体:[ ]
相关热点:

这是一个给新手学习代码的帖子,包含以下内容:
如何使用UBB代码,如何用js与剪贴板交互,如何使用textRange对象,如何使用自定义的快捷键操作,如何自动随窗口大小调整页面内容尺寸,正则表达式的使用等等

 

请仔细阅读代码,有问题请提问,目前代码开发完成度80%,IE only:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "standards-compliant mode" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
<head>
<meta http-equiv="content-type" content="text/HTML; charset=gb2312" />
<title>Blue Idea UBB Code Edit</title>
<style>
* {
 margin:0px;
 padding:0px;
}

HTML, body {
 background-color:buttonface;
 width:100%;
 height:100%;
 overflow:hidden;
 border-width:0px;
 font-size:13px;
 font-family:Verdana Arial 宋体;
 color:#333333;
 cursor:default;
}

input {border-width:1px;}

#divMain {
 overflow:hidden;
}

#divTools {
 width:100%;
 height:20px;
 line-height:20px;
 overflow:hidden;
 border-bottom:1px solid buttonshadow;
 margin:0px;
 padding:0px;
 padding-left:10px;
 background-color:buttonface;
 color:black;
}

#divTools label { font-weight:bold;float:left;padding-left:15px;padding-right:15px;}

#divTools a {
 display:block;
 float:left;
 height:20px;
 padding-left:15px;
 padding-right:15px;
 line-height:20px;
 text-align:center;
 overflow:hidden;
 color:black;
 text-decoration:none;
}

#divTools a:hover {background-color:gold;color:black;}

#divTools a:active {background-color:navy;color:white;}

#divContent, #txtContent {
 border:1px inset buttonface;
 width:94%;
 height:400px;
 overflow:auto;
 padding:5px;
 margin:2px 15px 0px 15px;
 background-color:white;
 color:black;
 display:none;
}

#txtContent {
 font-size:14px;
 border-width:1px;
 display:block;
}

.UBB_code {
 border:1px solid #555;
 padding:10px;
 margin:10px;
 width:500px;
 background-color:#DDD;
 font-size:13px;
 font-weight:normal;
 color:black;
 
}

.UBB_HTML {
 padding:4px;
 width:350px;
 height:200px;
 font-size:13px;
 font-weight:normal;
 color:black;
}

.UBB_HTML_button {
 width:100px;
 height:22px;
 line-height:18px;
 border-width:2px;
 margin:0px;
}
</style>
<script type="text/JavaScript">
var txtContent, divContent, viewButton01, viewButton02, dtf;

var strHTMLStart="<div><textarea class=\"UBB_HTML\">";
var strHTMLEnd="<\/textarea><input type=\"button\" value=\"运行代码\" onclick=\"UBB_runCode(this);\" class=\"UBB_HTML_button\" \/><\/div>";
var strCodeStart="<div class=\"UBB_code\">";
var strCodeEnd="<\/div>";

function init(){
 txtContent=document.getElementById("txtContent");
 divContent=document.getElementById("divContent");
 viewButton01=document.getElementById("viewButton01");
 viewButton02=document.getElementById("viewButton02");
 
 dtfFrame=document.createElement("iframe");
 dtfFrame.frameBorder="0px";
 dtfFrame.style.width="0px";
 dtfFrame.style.height="0px";
 document.body.appendChild(dtfFrame);
 dtf=window.frames[window.frames.length-1];
 dtf.location.href="about:blank";
 
 window.onresize=switchModel;
 txtContent.onkeydown=chkKey;
 
 switchModel(0);
}

function switchModel(id){
 // 重新计算输入区域大小
 txtContent.style.width=document.body.clientWidth-50+"px";
 txtContent.style.height=document.body.clientHeight-50+"px";
 divContent.style.width=document.body.clientWidth-50+"px";
 divContent.style.height=document.body.clientHeight-50+"px";
 if(id==0){ // 转到代码视图
  divContent.style.display="none";
  txtContent.style.display="block";
  viewButton01.style.backgroundColor="navy";
  viewButton01.style.color="white";
  viewButton02.style.backgroundColor="";
  viewButton02.style.color="";
  txtContent.focus();
 }else if(id==1){ // 转到预览视图
  divContent.style.display="block";
  txtContent.style.display="none";
  divContent.innerHTML=UBB2HTML(txtContent.value);
  viewButton02.style.backgroundColor="navy";
  viewButton02.style.color="white";
  viewButton01.style.backgroundColor="";
  viewButton01.style.color="";
  divContent.focus();
 }
}

function HTML2UBB(strHTML){
 var re=HTMLDecode(strHTML);
 re=re.replace(/<(\/?)strong>/ig,"[{GetProperty(Content)}b]");
 re=re.replace(/<(\/?)b>/ig,"[{GetProperty(Content)}b]");
 re=re.replace(/<(\/?)em>/ig,"[{GetProperty(Content)}i]");
 re=re.replace(/<(\/?)i>/ig,"[{GetProperty(Content)}i]");
 re=re.replace(/< *(\/?) *div[\w\W]*?>/ig,"\r\n");
 re=re.replace(/< *img +[\w\W]*?src=["]?([^">\r\n]+)[\w\W]*?>/ig,"[img]{GetProperty(Content)}[/img]");
 re=re.replace(/< *a +[\w\W]*?href=["]?([^">\r\n]+)[\w\W]*?>([\w\W]*?)< *\/ *a *>/ig,"[url={GetProperty(Content)}]$2[/url]");
 re=re.replace(/<script[\w\W]+?<\/script>/ig,"");
 re=re.replace(/<[\w\W]*?>/ig,"");
 re=re.replace(/(\r\n){2,}/g,"\r\n");
 return(re);
}

function UBB2HTML(strUBB){
 var re=strUBB;
 // 转换HTML实体
 re=HTMLEncode(re);
 // 屏蔽 HTML 和 code 中的 UBB 转意符
 re=re.replace(/(\[HTML\])([\w\W]*?)(\[\/HTML\])/ig,UBB2HTML_escapeUBB);
 re=re.replace(/(\[code\])([\w\W]*?)(\[\/code\])/ig,UBB2HTML_escapeUBB);
 // 转换UBB代码为HTML代码
 re=re.replace(/\[b\]/ig,"<b>").replace(/\[\/b\]/ig,"<\/b>");
 re=re.replace(/\[i\]/ig,"<i>").replace(/\[\/i\]/ig,"<\/i>");
 re=re.replace(/\[code\]/ig,strCodeStart).replace(/\[\/code\]/ig,strCodeEnd);
 re=re.replace(/\[HTML\]/ig,strHTMLStart).replace(/\[\/HTML\]/ig,strHTMLEnd);
 re=re.replace(/\[size=(\d)\]/ig,UBB2HTML_fontSize).replace(/\[\/size\]/ig,"<\/span>");
 re=re.replace(/\[img\]/ig,"<img src=\"").replace(/\[\/img\]/ig,"\" \/>");
 re=re.replace(/\[url\]([\w\W]+?)\[\/url\]/ig,"<a href=\"{GetProperty(Content)}\">{GetProperty(Content)}<\/a>");
 re=re.replace(/\[url=([^\]]+?)\]/ig,UBB2HTML_url).replace(/\[\/url\]/ig,"<\/a>");
 
 // 还原 HTML 和 code 中的 UBB 转意符
 re=re.replace(/\\\[/g,"[").replace(/\\\]/g,"]");
 return(re);
}

function UBB2HTML_escapeUBB(strAll,strS1,strS2,strS3){
 switch(strS1.toLowerCase()){
  case "[HTML]":
   return(strS1+HTMLDecode(strS2.replace(/\[/g,"\\[").replace(/\]/g,"\\]"))+strS3);
  break;
  case "[code]":
   return(strS1+strS2.replace(/\[/g,"\\[").replace(/\]/g,"\\]")+strS3);
  break;
 }
}

function HTMLEncode(strS){
 return(strS.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/ /g,"&nbsp;").replace(/\r\n/g,"<br\/>"));
}

function HTMLDecode(strS){
 return(strS.replace(/<br\/?>/ig,"\r\n").replace(/&nbsp;/ig," ").replace(/&gt



收藏本文 打印 打印本文  推荐本文 告诉好友 投稿 投稿邮箱

站长排行

学院

新闻

专栏

盈利

[揭密网络黄链]中国留学生买凶专破日本
windows 2008中文版iis7+asp+php+Acces
贴吧发帖机使用教程(绝对原创)
超强弹出窗口代码,什么都挡不住
Windows Vista局域网网络连接设置
JSP语法(6)
FLASH视觉特效实例之地震效果
关于数据分页(转自www.codeproject.co
教你如何在Vista中设置麦克风
不用Photoshop 在线恶搞大头像
全国哀悼日网友自发QQ隐身上线 向死难
淘宝网卖家公然叫卖“艳照门”照片集
抗震救灾/鱼目混珠?
Google绿色专家质疑黑色背景网页节省资
阿里妈妈广告卖主全攻略
电影小说类相关关键字搜索量列表
爆笑 阿里就这样把亿告征服!
大脚:实战单页面优化关键词之——淘宝
国内各IT企业办公环境揭秘(多图)
心系灾区,点燃希望——手机QQ2008祈福
ECSHOP模板制作参考文档
悬挂阿里妈妈会否被百度惩罚
ECSHOP模板下载
阿里妈妈是否是中小站长的救世主?
最强网店ECShop发新版 众多酷炫功能给
土豆网,优酷网,爆米花等视频网站采集
DedeCms模板安装/制作概述
网上商店系统巅峰对决 ECShop vs ShopE
编程中国全站采集规则
http://www.mt86.com全站采集规则
性福联盟 一个不尊重站长的联盟
百度网络广告利润增长将会持续多久
大脚:日赚100元—揭露最新firefox欺骗
分享经验:个人网站如何月赚1万!
大脚:垃圾站超级赚钱法之二—突破“站
迅雷联盟、快车联盟收入对比
大脚:垃圾站超级赚钱法之——前言
Google Adsense的秘密 第二版
风险投资公司vc联系方式名录大全(一)
经理人必看的十个管理网站
站长学院  网页设计 建站教程 图形图象 网络编程

制定完整的网站
制定完整的网站
中国企业网站的
中国企业网站的

网站运营之怎么样来粘住你的用
网麒:网站建设创意
怎样提高网页快照更新
两种方法,教你如何通过微软正
英文网站优化细节
利用SSL加密增强FTP服务器的安
网页设计常用Javascript经典代

新闻线索

如果你有站长界人事变动、重组并购、变革技术出现,以及产品投诉等重要新闻线索,请告诉我们,我们会给予特别关注。
0631-3653338
站长中国编辑部
站长中国24小时新闻热线: 13156089008