Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在google Pretified元素中保留html字符?_Javascript_Jquery_Syntax Highlighting - Fatal编程技术网

Javascript 在google Pretified元素中保留html字符?

Javascript 在google Pretified元素中保留html字符?,javascript,jquery,syntax-highlighting,Javascript,Jquery,Syntax Highlighting,我试图通过googleprettify和一些jQuery将语法突出显示纳入我的Tumblr博客 Google prettify需要 <script type='text/javascript'> $(document).ready(function() { $('pre').addClass('prettyprint'); prettyPrint(); }); </script> 这很好,但我希望能够复制并粘贴我想在Tumblr上显示的代码,而不

我试图通过
googleprettify
和一些jQuery将语法突出显示纳入我的Tumblr博客

Google prettify
需要

<script type='text/javascript'>
$(document).ready(function() {
      $('pre').addClass('prettyprint');
      prettyPrint();
});
</script>
这很好,但我希望能够复制并粘贴我想在Tumblr上显示的代码,而不是每次我想发布代码片段时都将所有
转换为

使用jQuery,我想用相应的html代码替换所有
,但只在
pre
元素中。但是,这可能会有一些问题,因为它需要在美化代码之前替换所有这些

我是否可以附加新类,替换标记,然后调用
prettyPrint()初始化所有内容?或者我应该在发布代码之前手动将html字符更改为htmlentities吗

小更新: 此脚本用于更改
,但
除外


$(函数(){
变量$pre=$('pre');
$pre.html($pre.html().replace(//g,'l'));
});

注意:我使用
f
作为替换,只是为了测试代码。

如果先用实体替换所有的符号,然后用小于号替换,则可以将返回的字符串用作
中的文本节点,或将其空白设置为“pre”的任何块容器

函数stripX(str){

返回str.replace(/\&(?)/g,“&;”).replace(/为什么我要用html实体替换所有的符号?
<script type='text/javascript'>
$(function() {
   var $pre = $('pre');
   $pre.html($pre.html().replace(/</g, 'f').replace(/>/g, 'l'));
});
</script>

<pre>
    <html></html>
    <body></body>
    <head><head>
    <i></i>
</pre>
function stripX(str){
    return str.replace(/\&(?!(\w+;))/g, '&amp;').replace(/</g, '&lt;');
}