如果等於則設為block,如果不等於這設為none,如果在將效果設置複雜一點,當點擊按鈕時,不是忽然隱藏和顯示子菜單,而是高度平滑的轉變,這時就要通過setTimeout來設置子菜單的height了。下面學步園小編來講解下Json怎樣擴展jQuery?
Json怎樣擴展jQuery
以前用習慣了擴展Jquery的Json,在進行ajax開發時,處理json數據非常方便;而在jQuery中,只提供了一個簡單的jQuery.getJSON()方法,並未提供jQuery本身對json數據的轉換處理,將json字元串轉換為javascript數據對象還比較容易利用eval()函數即可,但要將javascript的數據類型轉換成json字元串就比較難了;剛開始不得同
時使用prototype.js和jQuery。
擴展Jquery的Json代碼:
//擴展jQuery對json字元串的轉換
jQuery.extend({
/***@see將json字元串轉換為對象*@paramjson字元串*@return返回object,array,string等對象*/
evalJSON:function(strJson){
returneval("("+strJson+")");
}
});
jQuery.extend({
/***@see將javascript數據類型轉換為json字元串*@param待轉換對象,支持object,array,string,function,number,boolean,regexp*@return返回json字元串*/
Json怎樣擴展jQuery
toJSON:function(object){
vartype=typeofobject;
if('object'==type){
if(Array==object.constructor)type='array';
elseif(RegExp==object.constructor)type='regexp';
elsetype='object';
}
switch(type){
case'undefined':
case'unknown':
return;
break;
case'function':
case'boolean':
case'regexp':
returnobject.toString();
break;
case'number':
returnisFinite(object)?object.toString():'null';
break;
case'string':
return'"'+object.replace(/(\|")/g,"\$1").replace(/n|r|t/g,function(){
vara=arguments[0];
return(a=='n')?'\n':(a=='r')?'\r':(a=='t')?'\t':""
})+'"';
break;
case'object':
if(object===null)return'null';
varresults=[];
for(varpropertyinobject){
varvalue=jQuery.toJSON(object[property]);
if(value!==undefined)results.push(jQuery.toJSON(property)+':'+value);
}
return'{'+results.join(',')+'}';
break;
case'array':
varresults=[];
for(vari=0;i