您的位置:首页 > Web前端 > JavaScript

项目js函数大全

2017-03-15 17:17 351 查看
1.获取当前的日期时间格式“yyyy-MM-ddHH:MM:SS”

functiongetNowFormatDate(){
vardate=newDate();
varseperator1="-";
varseperator2=":";
varmonth=date.getMonth()+1;
varstrDate=date.getDate();
if(month>=1&&month<=9){
month="0"+month;
}
if(strDate>=0&&strDate<=9){
strDate="0"+strDate;
}
varcurrentdate=date.getFullYear()+seperator1+month+seperator1+strDate
+""+date.getHours()+seperator2+date.getMinutes()
+seperator2+date.getSeconds();
returncurrentdate;
}


2.jQuery表单序列化对象

$.fn.serializeObject=function(){
varobj={};
varcount=0;
$.each(this.serializeArray(),function(i,o){
varn=o.name,v=o.value;
count++;
obj
=obj
===undefined?v
:$.isArray(obj
)?obj
.concat(v)
:[obj
,v];
});
//obj.nameCounts=count+"";//表单name个数
returnJSON.stringify(obj);
};

调用示例:varserStr=$('#form1').serializeObject();


3.Js判断回文字符串

functionpalindrome(str){
//\W匹配任何非单词字符。等价于“[^A-Za-z0-9_]”。
varre=/[\W_]/g;
//将字符串变成小写字符,并干掉除字母数字外的字符
varlowRegStr=str.toLowerCase().replace(re,'');
//如果字符串lowRegStr的length长度为0时,字符串即是palindrome
if(lowRegStr.length===0)returntrue;
//如果字符串的第一个和最后一个字符不相同,那么字符串就不是palindrome
if(lowRegStr[0]!=lowRegStr[lowRegStr.length-1])returnfalse;
//递归
returnpalindrome(lowRegStr.slice(1,lowRegStr.length-1));
}


4.ES6函数组合

constpipe=(...fns)=>x=>fns.reduce((v,f)=>f(v),x);
constfn1=s=>s.toLowerCase();
constfn2=s=>s.split('').reverse().join('');
constfn3=s=>s+'!'

constnewFunc=pipe(fn1,fn2,fn3);
constresult=newFunc('Time');//emit!


5.jquery插件编写

$.fn.extend={
"alterBgColor":function(options){
options=$extend({
odd:'odd',
even:'even',
selected:'selected'
},option);
$('tbody>tr:odd',this).addClass(options.odd);
$('tbody>tr:even',this).addClass(options.even);
$('tbody>tr',this).click(function(){
varhasSelected=$(this).hasClass(options.selected)?true;flase;
$(this)[hasSelected?'removeClass':'addClass'](options.selected);
$(this).find('checkbox').attr('checked',!$(this).attr('checked'))

})
}
}
//table隔行变色插件


6.图片上传常用函数

<script>
//输出选中的文件列表相关的信息
functionfileinfo(files){
for(vari=0;i<files.length;i++){//files是一个类数组对象
varf=files[i];
//a.txt86text/plainMonSep19201611:07:43GMT+0800(中国标准时间)
console.log(f.name,//只是名字:没有路径
f.size,f.type,//size和type是Blob的属性
f.lastModifiedDate);//修改时间
}
}
</script>
<inputtype="file"onchange="fileinfo(this.files)"/>

functionshowPreview(source){
varfile=source.files[0];
console.log(file.type+'---'+file.size);
console.log(source.value);

if(window.FileReader){
varfr=newFileReader();
fr.onload=function(e){

varimage=newImage();
image.src=e.target.result;
console.log(e.target.result);//base64格式的图片路径
console.log(image.width+'---'+image.height);
if(image.width>900){
alert('照片太宽');
return
}
document.getElementById("portrait").src=e.target.result;

};
fr.readAsDataURL(file);
}
}


7.非常简单的获取url参数函数

functiongetUrlParam(sUrl,sKey){
varresult={};
sUrl.replace(/\??(\w+)=(\w+)&?/g,function(a,k,v){
if(result[k]!==void0){
vart=result[k];
result[k]=[].concat(t,v);
}else{
result[k]=v;
}
});
if(sKey===void0){
returnresult;
}else{
returnresult[sKey]||'';
}
}

getUrlParam('http://www.nowcoder.com?key=1&key=2&key=3&test=4#hehe','key')
//输出[1,2,3]


好久没更新了,主要是公司项目太tm赶了,老板催着折腾,来的2个总监都是走管理路线的,没有一点技术支撑,还要我们往死里干,已经辞了几个人,也逼走了几个人,我估计也快gg了,行了,就这么多吧,下回再出来分享.


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: