JavaScript开发技巧
1.在编写js代码时,应尽量避免全局变量的使用。如果实在需要使用全局变量,则可以使用一个function来规避全局变量的使用。
2.数字解析
1 //1.丢弃小数部分,保留整数部分 2 alert( parseInt(7/2) + ":3" ); 3 4 5 //2.向上取整,有小数就整数部分加1 6 alert( Math.ceil(7/2) +":4" ); 7 8 9 // 3,四舍五入. 10 alert( Math.round(7/2) + ":4"); 11 12 13 // 4,向下取整 14 alert( Math.floor(7/2) +":4");
3.encodeURI的妙用(encodeURI非中文编码 不起作用的)
1 function test(varcode){ 2 var code = varcode?varcode:"中国"; 3 var encode = encodeURI(code); 4 var enencode = encodeURI(encode); 5 alert(" encode="+encode+"\n"+"enencode="+enencode+"\n xxxx="+encodeURI(encodeURI(code))); 6 } 7 test("sdsds2323232");
4.页面宽高自适应
1 /*window resize*/ 2 winResize=function(){ 3 var winWidth = $(document.body).width(),winHeight=$(document.body).height(); 4 $("#js_layout").layout("resize",{width:winWidth,height:winHeight}); 5 $("#tree_penal2").css({width:winWidth-10,height:winHeight-62}); 6 }; 7 $(window).resize(winResize); 8 winResize();
5.对象数组,组装URL
1 queryParams.titleName = $("#titleNameSearch").val() 2 queryParams.createUserRealName = ""; 3 queryParams.typeId = $("#typeIdSearch").val(); 4 queryParams.beginDate =$("#beginDateSearch").val(); 5 queryParams.endDate = $("#endDateSearch").val(); 6 queryParams.wbCode =$("#wbCodeSearch").val(); 7 queryParams.phaseId = $("#phaseIdSearch").val(); 8 queryParams.subjectId =$("#wbsTitleIdSearch").val(); 9 //queryParams.newDate = new Date(); 10 queryParams.pSelect=pSelect; 11 queryParams.ctype=$("#ctypeSearch").val(); 12 scopeparam=queryParams; 13 var qparamUrl=""; 14 for(var a in queryParams){ 15 qparamUrl+="&"+a+"="+encodeURIComponent(queryParams[a]); 16 }
获取相应参数
1 var title = "${param.title}"; 2 var queryParams = { 3 pSelect:"${param.pSelect}", 4 titleName:"${param.titleName}", 5 typeId:"${param.typeId}", 6 wbCode:"${param.wbCode}", 7 phaseId:"${param.phaseId}", 8 subjectId:"${param.subjectId}", 9 beginDate:"${param.beginDate}", 10 endDate:"${param.endDate}" 11 };