|
源代码网推荐
这是一个给新手学习代码的帖子,包含以下内容:
如何使用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[wW]*?>/ig,"
"); re=re.replace(/< *img +[wW]*?src=["]?([^">
]+)[wW]*?>/ig,"[img]{GetProperty(Content)}[/img]"); re=re.replace(/< *a +[wW]*?href=["]?([^">
]+)[wW]*?>([wW]*?)< */ *a *>/ig,"[url={GetProperty(Content)}]$2[/url]"); re=re.replace(/<script[wW]+?</script>/ig,""); re=re.replace(/<[wW]*?>/ig,""); re=re.replace(/(
){2,}/g,"
"); return(re); }
function UBB2HTML(strUBB){ var re=strUBB; // 转换HTML实体 re=HTMLEncode(re); // 屏蔽 HTML 和 code 中的 UBB 转意符 re=re.replace(/([HTML])([wW]*?)([/HTML])/ig,UBB2HTML_escapeUBB); re=re.replace(/([code])([wW]*?)([/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]([wW]+?)[/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,"&").replace(/</g,"<").replace(/>/g,">").replace(/ /g," ").replace(/
/g,"<br/>")); }
function HTMLDecode(strS){ return(strS.replace(/<br/?>/ig,"
").replace(/
源代码网供稿. |