HTML标记的补全...
<!--
这个工作一般是从后台处理,不过给出JS代码很容易修改成若干语言的吧。。。
-->
<textarea>
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td> </td>
<td>
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td><img /></td>
<td> </td>
</tr>
<tr>
<td><param /> </td>
<td> </td>
</tr>
<tr>
<td></font> </td>
<td> </td>
</tr>
</textarea>
<textarea style="width:600px; height:300px;">
</textarea>
<script type="text/javascript">
var replenishHTML = function (string) {
var list = [];
var htmlPattern = /<(\/?)(?!(?:img|param|link|base)\b)([a-zA-Z]+)\b/g;
while (htmlPattern.test(string)) {
if (RegExp.$1) {
var temp = '';
for (var i = list.length, l = i ; i > -1 ; i --) {
if (list[i] === RegExp.$2) {
temp = '<\/' + list.splice(i, list.length - i).reverse().join('><\/') + '>';
break;
}
}
if (i == -1) temp = '<' + RegExp.$2 + '><\/' + RegExp.$2 + '>';
string = RegExp.leftContext + temp + RegExp.rightContext.slice(1);
htmlPattern.lastIndex = RegExp.leftContext.length + temp.length;
} else {
list.push(RegExp.$2);
}
}
if (list.length) string = string + '<\/' + list.splice(0, list.length).reverse().join('><\/') + '>';
return string;
};
window.onload = function () {
document.getElementsByTagName('textarea')[1].value = replenishHTML(document.getElementsByTagName('textarea')[0].value);
};
</script>