在我这次制作Blog的新外观时,半于文章标题的字体设置一直不满意,不是宋体或者微软雅黑的英文难看,就是放大之后的宋体难看,于是利用JQuery写了下面的代码实现了中文用微软雅黑,英文用CalibriCalibri字体是难得的在12像素大小时看起来像11像素的字体。,实现的原理如下:

首先设置中文字体,然后将0x20~0x800x20~0x80是常见的所有半角字符之间的字符找出来,为它们加上i标签,而这里的标签i又被我设置成了英文字体Calibri,于是就达到目的了。

  1. $(".post .title a").each(function(){ 
  2.     var oldHtml=this.innerHTML+""
  3.     var newHtml=oldHtml+""
  4.     var regex= new RegExp("[\\x20-\\x80]+","g"); 
  5.      
  6.     do
  7.         var match=regex.exec(oldHtml);             
  8.      
  9.         if(match!=null
  10.         { 
  11.             regex.lastIndex=match.index+match[0].length; 
  12.             newHtml=newHtml.replace(match[0],"<i>"+match[0]+"</i>"); 
  13.         } 
  14.     } 
  15.     while(match!=null)         
  16.     this.innerHTML=newHtml; 
  17. });