zoukankan      html  css  js  c++  java
  • 过滤掉除img和input标签以外的所有其他标签

    <!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; 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);                   
                                               
                    })
                                           
               })       

  • 相关阅读:
    60款很酷的 jQuery 幻灯片演示和下载
    分享27款非常棒的 jQuery 表单插件
    10款新鲜出炉的 jQuery 插件
    5 个很炫的 HTML5 游戏
    12款高质量的免费 HTML 网页模板下载
    jquery插件整理篇(四)自动补全类插件
    jquery插件整理篇(三)图片展示插件
    超强1000个jquery插件!
    34个漂亮的应用程序后台管理系统界面(系列二)
    jquery插件整理篇(九)数据验证类
  • 原文地址:https://www.cnblogs.com/dearxinli/p/2818350.html
Copyright © 2011-2022 走看看