DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5教程
簡單實作 HTML 轉 UBB 的轉換程式
編輯:HTML5教程     

這個小工具的主要目的,是讓 UBB 貼文時能更方便一些。它能自動辨識並轉換圖片、超連結、字體顏色,以及粗體、斜體、底線等 HTML 代碼,轉換成對應的 UBB 代碼,同時相容 IE 與 Mozilla 等多種瀏覽器。

它的運作流程大致如下:使用者在可編輯的 iframe 區域內貼上或輸入內容,按下「轉換」按鈕後,程式會取出編輯區的 HTML,透過一系列正規表達式比對並替換,最後輸出成 UBB 代碼;「清除」按鈕則可清空輸出結果,而「查看源代碼」核選框能在所見即所得的編輯模式與源碼檢視模式之間切換。

以下為完整的原始碼:

<html>
<head>
<title>Html轉換程序</title>
<SCRIPT language="JavaScript1.2">
function HtmlToUBB(str) {
str = str.replace(/r/g,"");
str = str.replace(/on(load|click|dbclick|mouseover|mousedown|mouseup)="[^"]+"/ig,"");
str = str.replace(/<script[^>]*?>([wW]*?)</script>/ig,"");
str = str.replace(/<a[^>]+href="([^"]+)"[^>]*>(.*?)</a>/ig,"n[url=$1]$2[/url]n");
str = str.replace(/<font[^>]+color=([^ >]+)[^>]*>(.*?)</font>/ig,"n[color=$1]$2[/color]n");
str = str.replace(/<img[^>]+src="([^"]+)"[^>]*>/ig,"n[img]$1[/img]n");
str = str.replace(/<([/]?)b>/ig,"[$1b]");
str = str.replace(/<([/]?)strong>/ig,"[$1b]");
str = str.replace(/<([/]?)u>/ig,"[$1u]");
str = str.replace(/<([/]?)i>/ig,"[$1i]");
str = str.replace(/ /g," ");
str = str.replace(/&/g,"&");
str = str.replace(/"/g,""");
str = str.replace(/</g,"<");
str = str.replace(/>/g,">");
str = str.replace(/<br>/ig,"n");
str = str.replace(/<[^>]*?>/g,"");
str = str.replace(/[url=([^]]+)]n([img]1[/img])n[/url]/g,"$2");
str = str.replace(/n+/g,"n");
return str;
}
function trans(){
var str = "";
str = document.getElementById('edit').contentWindow.document.body.innerHTML;
if (str.length == 0) {
alert("無轉換內容!請使用 Ctrl+V 把內容複製到下面的編輯框內");
}
document.getElementById('ReCode').value=HtmlToUBB(str);
}
function clearCode(){
document.getElementById('ReCode').value="";
}
function Start() {
document.getElementById('edit').contentWindow.document.designMode = "on";
try {
document.getElementById('edit').contentWindow.document.execCommand("undo", false, null);
} catch (e) {
alert("你的 Mozilla 瀏覽器不支援在線 RichEditor");
}
}
function ViewSource(Source){
var Html;
if (Source){
document.getElementById('sCode').value = document.getElementById('edit').contentWindow.document.body.innerHTML;
document.getElementById('edit').style.display="none";
document.getElementById('sCode').style.display="";
document.getElementById('tip').innerText="修改粘貼後的網頁源代碼 >>";
document.getElementById('tranButton').disabled="disabled"
}else{
document.getElementById('edit').contentWindow.document.body.innerHTML = document.getElementById('sCode').value;
document.getElementById('edit').style.display="";
document.getElementById('sCode').style.display="none";
document.getElementById('edit').contentWindow.document.designMode = "on";
document.getElementById('tip').innerText="請使用 Ctrl+V 把內容複製到這裡 >>";
document.getElementById('tranButton').disabled=""
}
}
</SCRIPT>
</head>
<body onload="Start()">
<table border="0" align="center">
<tr>
<th>Html轉換UBB程序</th>
</tr>
<tr>
<td>
<label for="Source"><input type="checkbox" id="Source" onclick="ViewSource(this.checked)">查看源代碼 </label>
<input id="tranButton" type="button" value="轉 換" onclick="trans()">
<input type="button" value="清 除" onclick="clearCode()"></td>
</tr>
<tr>
<td><span id="tip">請使用 Ctrl+V 把內容複製到這裡 >></span></td>
</tr>
<tr>
<td>
<iframe id="edit" src="about:blank" frameborder="0"></iframe>
</td>
</tr>
<tr>
<td>轉換輸出代碼 >></td>
</tr>
<tr>
<td>
<textarea id="ReCode" readonly="readonly"></textarea>
</td>
</tr>
</table>
</body>
</html>

整體而言,這支程式以 iframe 的 designMode 提供所見即所得的編輯體驗,再透過正規表達式把常見的 HTML 標籤逐一對應到 UBB 語法,對於需要把富文本內容貼進論壇或留言板的情境來說,是個相當實用的小幫手。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved