js一些常用方法
1、转码解码
encodeURIComponent、decodeURIComponent
2、拆分,浏览器url拆分好用
如网页url:http://www.txf.com/index.html#%E5%9B%9B%E5%B9%B3%E8%B7%AF-2-1-0-0-0-0-1
var mdurl=location.hash.substr(1);
urlstrs=mdurl.split("-");
3、检查字符串是否包含 indexOf()
如果要检索的字符串值没有出现,则该方法返回 -1。
4、slice
截取后几位:xxx.slice(-4)
截取前几位:xxx.slice(0,6)
5、算页数
var 总页数=Math.ceil(总条数/每页条数);
6、jquery判断数据是否为空
jQuery.isEmptyObject(obj)
7、验证
var reg=/^\d+$/;//验证数字
var phonereg = /(^1[34578]\d{9}$)/g;
phonereg.test(nameval)
8、ajax请求
$.post(
"/web/api/login",
{"source":"web"},
function(res){//ajax获取数据
if(res.stateCode==200){
}else{
};
};
);
$.getJSON(
"/web/api/login",
{"source":"web"},
function(res){//ajax获取数据
if(res.stateCode==200){
}else{
};
};
);
9、jquery.cookie
设置 $.cookie("cityname",ipcity,{expires:30,path: "/"});
使用 $.cookie("yazhu_user")
验证:typeof($.cookie("yazhu_user"))!="undefined"
占位