网页上贴代码,如何实现高亮和折叠?
好像是通过JS实现的。
有没有这样的JS?
谢谢

解决方案 »

  1.   

    <?php
    $s = highlight_string('程序代码',1); //或
    $s = highlight_file('程序文件.php',1);echo preg_replace_callback('/[{}]/', 'foo', $s);function foo($v) {
      static $deep = 0;
      switch($v[0]) {
        case '{':
          $r = "{<div id='a$deep' onclick=show('a$deep','b$deep')>...</div><div id='b$deep' style='display:none' onclick=show('b$deep','a$deep')>";
          $deep++;
          break;
        case '}':
          $r = '</div>}';
          break;
      }
      return $r;
    }
    echo <<< JS
    <script>
    function show(a, b) {
      document.getElementById(a).style.display = 'none';
      document.getElementById(b).style.display = '';
    }
    </script>
    JS;
    ?>
    简单的实现