本帖最后由 default7 于 2011-08-25 23:23:23 编辑

解决方案 »

  1.   

    正则不方便,我所以才选择用Jquery的。
    我以及将我写的那个HTML都上传到了 http://it.zbphp.com/CSDN/ErrJqueryDOM/ 这个目录里面了,麻烦帮我看下……
      

  2.   


    可以打开这个 http://it.zbphp.com/CSDN/ErrJqueryDOM/ 地址来看下,里面的源码都在里面的。你看下演示地址: http://it.zbphp.com/CSDN/ErrJqueryDOM/ 
      

  3.   

    我修改了下,还是用的jQuery DOM来替换的,差不多就是这个效果,正则的不好:/* 传入OBJ */
    function FormatHtml(HTML){
      if(HTML.length == 0) return '';  /* 去除所有样式 */
      HTML.find('*').removeAttr('style')
                    .removeAttr('class');
      HTML.find('img').remove();  HTML.find('div').replaceWith(function(i,str){
        $(this).html( $.trim( $(this).html() ) );
        if($(this).html().substring(0,3) == '<p>'){
           return $(this).html();
        }else{
           return '<p>' + $(this).html() + '</p>';
        }
      });  /* 过滤回车BR */
      HTML.find('br').replaceWith(function(){
        return '<br />';
      });  /* 过滤样式 */
      HTML.find('p').replaceWith(function(){
        return '<p>'+ $.trim( $(this).html() )+'</p>';
        });  /* 连接过滤[不要图片之类的] */
      HTML.find('a').replaceWith(function(){
        // 51job.com  字符串长度为 9
        if($(this).attr('href').substring(0,4) == 'http' && $(this).attr('host').substr(-9,9).toLowerCase() != '51job.com'){
          return '<a href="'+$(this).attr('href')+'" target="_blank">'+ $.trim( $(this).text() )+'</a>';
        }else{
          return '';
        }
      });  return  HTML;
    }