今日写页面的时候需要用到字符串比较来设定input的状态,大概代码如下:

$("div.room_center_hd").delegate("input","focus",function(){

if(/搜索多功能厅/.exec($(this).val())){

$(this).val("").css("color","black");

}

}).delegate("input","blur",function(){

if(/^\s*$/.exec($(this).val())){

$(this).val("搜索多功能厅").css("color","#c8c8c8");

}

});

其中字符串比较用了正则表达式,是因为我通过测试代码如下

/*

测试:正则表达式匹配速度

console.time("a");

for(var i =0;i<100;i++){

if($(this).val() == "搜索多功能厅"){

console.info(i);

}

}

console.timeEnd("a");

console.time("b");

for(var i =0;i<100;i++){

if(/搜索多功能厅/.exec($(this).val())){

console.info(i);

}

}

console.timeEnd("b");

*/

发现使用正则表达式执行100次比传统的字符串直接比较少了6~7毫秒。

google了一下字符串比较的原理,只看到泪流满面的汇编的测试代码,估计我这辈子都看不懂了。。

先放着。。。