1.toFixed(num) 方法可把 Number 四舍五入为指定小数位数(num)的数字。
2.substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。stringObject.substr(start,length)
3. $.cookie('yynum', yynum, {path: '/', domain: 'yy.com'});
4.//格式化数字51200 51,200
function formatNumberByComma(num) {
if (isNaN(num))return num;
var numArry = String(num).split(".");
var ss = numArry[0];
var strFormat = "";
while (ss.length > 3) {
strFormat = "," + ss.substring(ss.length - 3, ss.length) + strFormat;
ss = ss.substring(0, ss.length - 3);
}
if (ss.length > 0) {
strFormat = ss + strFormat;
if (numArry[1]) {
strFormat += "." + numArry[1];
}
}
return strFormat;
}
5.js中获取json的值:
var o = {"name":"value"},o.name和o["name"]都可以取到value的值
ajax中处理返回的json字符串,将它转化为js对象:eval再加上两边的括号和引号,例:
eval("("+xhr.responseText+")");
localStorage存取json:
var staff = {};
staff.name = 'tomato';
staff.staff_passwd = 'lc';
localStorage.setItem("staff",JSON.stringify(staff));
console.log(JSON.parse( localStorage.getItem("staff"))) ;
6.倒计时实现
setInterval(function(){
var now = new Date(); //当前日期
var day = now.getDay(); //从 Date 对象返回一周中的某一天 (0 ~ 6)
var hour = now.getHours(); //返回 Date 对象的小时 (0 ~ 23)
var min = now.getMinutes(); //返回 Date 对象的分钟 (0 ~ 59)
if (day == 0) {
day = 7;//周日为第7天
}
day = 7 - day;
hour = 23 - hour;
min = 59 - min;
var dayHtml = '<i>0</i><i>'+ day + '</i>天';
var hourHtml = '<i>0</i><i>' + hour + '</i>时';
if (9 < hour) {
hour = hour.toString();
hourHtml = '<i>' + hour.substr(0, 1) + '</i><i>' + hour.substr(1, 1) + '</i>时';
}
var minHtml = '<i>0</i><i>' + min + '</i>分';
if (9 < min) {
min = min.toString();
minHtml = '<i>' + min.substr(0, 1) + '</i><i>' + min.substr(1, 1) + '</i>分';
}
$("#daysLeft").html(dayHtml);
$("#hoursLeft").html(hourHtml);
$("#minsLeft").html(minHtml);
},1000);
7.js数组排序:
arrDemo.sort(function(a,b){return a>b?1:-1});//从小到大排序
arrDemo.sort(function(a,b){return a<b?1:-1});//从大到小排序
8.javascript除法如何取整
Math.round(x) 四舍五入,如Math.round(0.60),结果为1;Math.round(0.49),结果为0;
Math.floor(x) 向下舍入,如Math.floor(0.60)与Math.floor(0.49),结果均为0;
Math.ceil(x)向上舍入,如Math.ceil(0.60)与Math.ceil(0. 49),结果均为1。
9.浏览器opacity兼容
/* older safari/Chrome browsers */
-webkit-opacity: 0.5;
/* Netscape and Older than Firefox 0.9 */
-moz-opacity: 0.5;
/* Safari 1.x (pre WebKit!) 老式khtml内核的Safari浏览器*/
-khtml-opacity: 0.5;
/* IE9 + etc...modern browsers */
opacity: .5;
/* IE 4-9 */
filter:alpha(opacity=50);
/*This works in IE 8 & 9 too*/
-ms-filter:progid:DXImageTransform.Microsoft.Alpha(Opacity=50);
/*IE4-IE9*/
filter:progid:DXImageTransform.Microsoft.Alpha(Opacity=50);
If you want opacity to also work in IE8′s emulating IE7 mode, the order should be:
-ms-filter:”progid:DXImageTransform.Microsoft.Alpha(Opacity=50)”; // first
filter: alpha(opacity=50); // second
10.Array截取子数组
array.slice(start,end);//实际截取下标为start到end-1的元素