Javascript html替换标签不起作用

时间:2016-06-07 18:08:17

标签: javascript jquery html css

我一直在努力替换我网站上发布的所有代码,但不适合我。 我需要<pre><code></code></pre>中出现的所有内容来重放所有标签,并使用css给它一个颜色,这里是我一直试图使用的代码示例

function Rep(text){
  text = text.replace(/ /g,"&nbsp;");

  text = text.replace(/<([^\>]+?)>/g, function(match, contents){
      return "<span class='TagColor'>&lt;"+contents+"&gt;</span>";
  }  
  );

  text = text.replace(/\"([^\"]+?)\"/g, function(match, contents){
      return '<span class="StringColor">&quot;'+contents+'&quot;</span>';
    }  
  );
  text = text.replace(/\(([^\)]+?)\)/g, function(match, contents){
      return '<span class="OptionsColor">('+contents+')</span>';
    }  
  );
  text = text.replace(/\n/g, function(match, contents){
      return '<br />';
  }  
  );
  text = text.replace(/\{/g, function(match, contents){
      return '<span class="BracketsColor">&#123;</span>';
  }
  );
  text = text.replace(/\}/g, function(match, contents){
      return '<span class="BracketsColor">&#125;</span>';
  }
  );
  text = text.replace(/\/\*([^\*]+?)\*\//g, function(match, contents){
      return "<span class='CommentColor'>/*"+contents+"*/</span>";
  }  
  );
    text = text.replace(/\[\](.*?)\[\/\]/g, function(match, contents){
      return '<span class="BBColor">'+contents+'</span>';
  }
  );
  return text;

}

//HTML replace
//==================================================
$('code').each(function(){
    var text=$(this).html().replace(/</g,'&lt;');
    var text=text.replace(/>/g,'&gt;');
    $(this).html(text);
});

//google-code-prettify
//==================================================
jQuery(window).load(function(){
    prettyPrint();
});

//Still on Text format                                                                                                                

var app = angular.module('codelab',[],['$interpolateProvider',
    function($interpolateProvider) {
        $interpolateProvider.startSymbol('[[');
        $interpolateProvider.endSymbol(']]');
        }]);

app.directive('ngPrism',['$interpolate', function ($interpolate) {
        "use strict";
        return {
          restrict: 'E',
          template: '<pre id="Mpreditor" class="code code-codes language-javascript"><code ng-transclude="ng-transclude" class="language-javascript"> </code></pre>',
          replace:true,
          transclude:true
        };
    }]);

以下是代码在我的网站中的显示方式

    <pre id="Mpreditor" class="code code-codes language-javascript">
    <code ng-transclude="ng-transclude" class="language-javascript">    
<span>Hello</span>
You have to include the `%` signs in the `$params`, not in the query:

        $query = "SELECT * FROM tbl WHERE address LIKE ? OR address LIKE ?";
        $params = array("%$var1%", "%$var2%");
        $stmt = $handle-&gt;prepare($query);
        $stmt-&gt;execute($params);
        </code></pre>

1 个答案:

答案 0 :(得分:0)

您应该考虑使用https://jmblog.github.io/color-themes-for-google-code-prettify/,它基本上完全符合您的要求。

它利用Googles Code Prettifier:https://github.com/google/code-prettify