HTML标记的补全...

<!--
这个工作一般是从后台处理,不过给出JS代码很容易修改成若干语言的吧。。。
-->
<textarea>
<table width="100%" cellspacing="0" cellpadding="0">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>
        <table width="100%" cellspacing="0" cellpadding="0">
        <tr>
        <td>&nbsp;</td>
        <td>
       
<table width="100%" cellspacing="0" cellpadding="0">
  <tr>
    <td><img /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><param />&nbsp;</td>
    <td>&nbsp;</td>
       
        </tr>
        <tr>
        <td></font>&nbsp;</td>
        <td>&nbsp;</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>

posted @ 2008-11-19 14:55  越兔  阅读(670)  评论(1编辑  收藏  举报