博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

过滤掉除img和input标签以外的所有其他标签

Posted on 2012-12-14 17:01  人生梦想起飞  阅读(2587)  评论(0编辑  收藏  举报

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>

</head>

<body>
 <div id="test" style="float: left; height: 100px; width: 500px; border:1px solid red" contenteditable="true" class="testmr"> 2222</div>

</body>

</html>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function(){

var update = function(original){
   // var  re = /<(?!(\/?img))[^>]+>/ig;//要保留其他标签修改这里
        var  re = /<(?!img|input).*?>/ig;//保留img和input标签
       
           if($(this).find('img').length>0){
       
         $(this).find('img').each(function(){
       // alert($(this).attr('src').toString().substr(0,44));
             if($(this).attr('src').toString().substr(0,44)=='http://res1.starheld.com:8080/group2/M00/0B/'){
          
           return;
          
         }else{
          
          $(this).remove();
          
         }
         
         })
        
         }
       
          if($(this).find('input').length>0){
       
         $(this).find('input').each(function(){
                 
             if($(this).attr('class')=='fill_empty'){
          
           return;
          
         }else{
          
          $(this).remove();
          
         }
         
         })
        
         }
      
        var new_content = $(this).html();
        var new_content = new_content.replace(re,"");//去掉除特定的img和和input标签以外的所有标签
        new_content = new_content.replace(/&nbsp;/ig,'');//去掉&nbsp;
      
      // new_content = new_content.replace(/<\/?.+?>/g,"");//去掉所有标签
      
       /* new_content = new_content.replace(/[ | ]*\n/g,'\n'); //去除行尾空白
       
        new_content = new_content.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
     
     
       new_content = new_content.replace(/<h1.*?>(.*?)<\/h1>/ig,"$1");
      
        new_content = new_content.replace(/<div.*?>(.*?)<\/div>/ig,"$1");
      
       new_content = new_content.replace(/<h2.*?>(.*?)<\/h2>/ig,"$1");
      
       new_content = new_content.replace(/<h3.*?>(.*?)<\/h3>/ig,"$1");
      
       new_content = new_content.replace(/<h4.*?>(.*?)<\/h4>/ig,"$1");
      
       new_content = new_content.replace(/<h5.*?>(.*?)<\/h5>/ig,"$1");
      
       new_content = new_content.replace(/<h6.*?>(.*?)<\/h6>/ig,"$1");
      
       new_content = new_content.replace(/style\="[^"]+"/gi, '');*/
      
      
      $(this).html(new_content);  }
  
  $('.testmr').bind('paste',function(e){
   var $this = $(this); 
   var original = $this.html();
   setTimeout(function(){update();}, 10);
   
    })
  })

</script>

 

 

 

和上面同样的功能

    //过滤
            
        $('.editor_question_item').live('focus',function(){
                                        
                var key=this.id;
            
            //多选题
            
            var updates = function(){
                
                $('#'+key).find('.editor_input_div_guolv').each(function(){
                            
                             if($(this).find('img').length>0){
                            
                              $(this).find('img').each(function(){
                                                                
                                  if($(this).attr('src').toString().substr(0,44)=='http://res1.starheld.com:8080/group2/M00/0B/'){
                                      
                                      return;
                                      
                                    }else{
                                        
                                        $(this).remove();
                                        
                                    }
                                  
                              })
                              
                           }
                          
                            if($(this).find('input').length>0){
                            
                              $(this).find('input').each(function(){
                                                                  
                                  if($(this).attr('class')=='fill_empty'){
                                      
                                      return;
                                      
                                    }else{
                                        
                                        $(this).remove();
                                        
                                    }
                                  
                              })
                              
                           }
                        
                          var new_content = $(this).html();
                          
                          
                          var reg_list = [/<\/?[wm]:([^>]+)\/?>([^<]+<\/[wm]:\1>)?/ig, /<(style)>[^<]+<\/\1>/ig, /<(?!img|input)[^>]+>/ig];
                          $.each(reg_list, function(i, reg){
                                new_content    = new_content.replace(reg, '');                    
                          });
                          
                        $(this).html(new_content);
                        
                        
                    })
              }
               $('#EditorArea').find('#'+key).find('.editor_input_div_guolv').bind('paste',function(){
                      
                        setTimeout(function(){updates();}, 10);                   
                                           
                })
                                       
           })