首页 > 开发 > JS > 正文

JavaScript版代码高亮

2024-09-06 12:45:17
字体:
来源:转载
供稿:网友

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript版代码高亮</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div class="menu_head">JavaScript版代码高亮显示</div>
<div class="content">
<script language="JavaScript" type="text/javascript">
/**
** ======================================================================================
** 类名:CLASS_HIGHLIGHT
** 功能:语法高亮
** 示例:
 var xx  = new CLASS_HIGHLIGHT(code,syntax);
 document.getElementById("display").innerHTML = xx.highlight();
** ======================================================================================
**/

function CLASS_HIGHLIGHT(code,syntax){
 //哈希表类
 function Hashtable(){
  this._hash = new Object();
  this.add = function(key,value){
   if(typeof(key)!="undefined"){
    if(this.contains(key)==false){
     this._hash[key]=typeof(value)=="undefined"?null:value;
     return true;
    } else {
     return false;
    }
   } else {
    return false;
   }
  }
  this.remove  = function(key){delete this._hash[key];}
  this.count  = function(){var i=0;for(var k in this._hash){i++;} return i;}
  this.items  = function(key){return this._hash[key];}
  this.contains = function(key){return typeof(this._hash[key])!="undefined";}
  this.clear  = function(){for(var k in this._hash){delete this._hash[k];}}
 }

 this._caseSensitive = true;

 //字符串转换为哈希表
 this.str2hashtable = function(key,cs){
  var _key = key.split(/,/g);
  var _hash = new Hashtable();
  var _cs  = true;

  if(typeof(cs)=="undefined"||cs==null){
   _cs = this._caseSensitive;
  } else {
   _cs = cs;
  }

  for(var i in _key){
   if(_cs){
    _hash.add(_key[i]);
   } else {
    _hash.add((_key[i]+"").toLowerCase());
   }
  }
  return _hash;
 }

 //获得需要转换的代码
 this._codetxt = code;
 if(typeof(syntax)=="undefined"){
  syntax = "";
 }

 switch(syntax.toLowerCase()){
  case "sql":
   //是否大小写敏感
   this._caseSensitive = false;

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表